]>
Commit | Line | Data |
---|---|---|
40d13c28 | 1 | <? |
f1a80dae AD |
2 | session_start(); |
3 | ||
40d13c28 | 4 | require_once 'config.php'; |
b619ff15 | 5 | require_once 'db-prefs.php'; |
40d13c28 | 6 | |
a3ee2a38 AD |
7 | define('MAGPIE_OUTPUT_ENCODING', 'UTF-8'); |
8 | ||
fefa6ca3 | 9 | function purge_feed($link, $feed_id, $purge_interval) { |
4c193675 AD |
10 | |
11 | return; // FIXME disabled for now | |
fefa6ca3 AD |
12 | |
13 | if (DB_TYPE == "pgsql") { | |
14 | db_query($link, "DELETE FROM ttrss_entries WHERE | |
15 | marked = false AND feed_id = '$feed_id' AND | |
16 | date_entered < NOW() - INTERVAL '$purge_interval days'"); | |
17 | } else { | |
18 | db_query($link, "DELETE FROM ttrss_entries WHERE | |
19 | marked = false AND feed_id = '$feed_id' AND | |
20 | date_entered < DATE_SUB(NOW(), INTERVAL $purge_interval DAY)"); | |
21 | } | |
22 | } | |
23 | ||
24 | function global_purge_old_posts($link, $do_output = false) { | |
25 | ||
26 | $result = db_query($link, | |
27 | "SELECT id,purge_interval,owner_uid FROM ttrss_feeds"); | |
28 | ||
29 | while ($line = db_fetch_assoc($result)) { | |
30 | ||
31 | $feed_id = $line["id"]; | |
32 | $purge_interval = $line["purge_interval"]; | |
33 | $owner_uid = $line["owner_uid"]; | |
34 | ||
35 | if ($purge_interval == 0) { | |
36 | ||
37 | $tmp_result = db_query($link, | |
38 | "SELECT value FROM ttrss_user_prefs WHERE | |
39 | pref_name = 'PURGE_OLD_DAYS' AND owner_uid = '$owner_uid'"); | |
40 | ||
41 | if (db_num_rows($tmp_result) != 0) { | |
42 | $purge_interval = db_fetch_result($tmp_result, 0, "value"); | |
43 | } | |
44 | } | |
45 | ||
46 | if ($do_output) { | |
47 | print "<feed id='$feed_id' p_intl='$purge_interval'/>"; | |
48 | } | |
49 | ||
50 | if ($purge_interval > 0) { | |
51 | purge_feed($link, $feed_id, $purge_interval); | |
52 | } | |
53 | } | |
54 | ||
55 | } | |
56 | ||
b6eefba5 | 57 | function purge_old_posts($link) { |
5d73494a | 58 | |
f1a80dae AD |
59 | $user_id = $_SESSION["uid"]; |
60 | ||
61 | $result = db_query($link, "SELECT id,purge_interval FROM ttrss_feeds | |
62 | WHERE owner_uid = '$user_id'"); | |
5d73494a AD |
63 | |
64 | while ($line = db_fetch_assoc($result)) { | |
65 | ||
66 | $feed_id = $line["id"]; | |
67 | $purge_interval = $line["purge_interval"]; | |
68 | ||
b619ff15 | 69 | if ($purge_interval == 0) $purge_interval = get_pref($link, 'PURGE_OLD_DAYS'); |
5d73494a | 70 | |
140aae81 | 71 | if ($purge_interval > 0) { |
fefa6ca3 | 72 | purge_feed($link, $feed_id, $purge_interval); |
5d73494a AD |
73 | } |
74 | } | |
c3a8d71a AD |
75 | } |
76 | ||
9c9c7e6b | 77 | function update_all_feeds($link, $fetch) { |
40d13c28 | 78 | |
4769ddaf | 79 | if (WEB_DEMO_MODE) return; |
b0b4abcf | 80 | |
b619ff15 | 81 | if (get_pref($link, 'DAEMON_REFRESH_ONLY')) { |
c5142cca AD |
82 | if (!$_GET["daemon"]) { |
83 | return; | |
84 | } | |
c70d731e AD |
85 | } |
86 | ||
b6eefba5 | 87 | db_query($link, "BEGIN"); |
b82af8c3 | 88 | |
f1a80dae AD |
89 | $user_id = $_SESSION["uid"]; |
90 | ||
d148926e AD |
91 | $result = db_query($link, "SELECT feed_url,id, |
92 | substring(last_updated,1,19) as last_updated, | |
f1a80dae | 93 | update_interval FROM ttrss_feeds WHERE owner_uid = '$user_id'"); |
40d13c28 | 94 | |
b6eefba5 | 95 | while ($line = db_fetch_assoc($result)) { |
d148926e AD |
96 | $upd_intl = $line["update_interval"]; |
97 | ||
b619ff15 AD |
98 | if (!$upd_intl || $upd_intl == 0) { |
99 | $upd_intl = get_pref($link, 'DEFAULT_UPDATE_INTERVAL'); | |
100 | } | |
d148926e | 101 | |
93d40f50 AD |
102 | if ($fetch || (!$line["last_updated"] || |
103 | time() - strtotime($line["last_updated"]) > ($upd_intl * 60))) { | |
c5142cca | 104 | |
8143ae1f | 105 | update_rss_feed($link, $line["feed_url"], $line["id"]); |
d148926e | 106 | } |
40d13c28 AD |
107 | } |
108 | ||
b6eefba5 | 109 | purge_old_posts($link); |
c3a8d71a | 110 | |
b6eefba5 | 111 | db_query($link, "COMMIT"); |
b82af8c3 | 112 | |
40d13c28 AD |
113 | } |
114 | ||
9e997874 | 115 | function check_feed_favicon($feed_url, $feed, $link) { |
78800912 AD |
116 | $feed_url = str_replace("http://", "", $feed_url); |
117 | $feed_url = preg_replace("/\/.*$/", "", $feed_url); | |
118 | ||
119 | $icon_url = "http://$feed_url/favicon.ico"; | |
273a2f6b | 120 | $icon_file = ICONS_DIR . "/$feed.ico"; |
78800912 AD |
121 | |
122 | if (!file_exists($icon_file)) { | |
e695fdc8 | 123 | |
78800912 AD |
124 | error_reporting(0); |
125 | $r = fopen($icon_url, "r"); | |
126 | error_reporting (E_ERROR | E_WARNING | E_PARSE); | |
127 | ||
128 | if ($r) { | |
129 | $tmpfname = tempnam("/tmp", "ttrssicon"); | |
130 | ||
131 | $t = fopen($tmpfname, "w"); | |
132 | ||
133 | while (!feof($r)) { | |
134 | $buf = fread($r, 16384); | |
135 | fwrite($t, $buf); | |
136 | } | |
137 | ||
138 | fclose($r); | |
139 | fclose($t); | |
140 | ||
e695fdc8 AD |
141 | error_reporting(0); |
142 | if (!rename($tmpfname, $icon_file)) { | |
143 | unlink($tmpfname); | |
144 | } | |
717f5e64 AD |
145 | |
146 | chmod($icon_file, 0644); | |
147 | ||
e695fdc8 | 148 | error_reporting (E_ERROR | E_WARNING | E_PARSE); |
78800912 AD |
149 | |
150 | } | |
151 | } | |
152 | } | |
153 | ||
40d13c28 AD |
154 | function update_rss_feed($link, $feed_url, $feed) { |
155 | ||
4769ddaf | 156 | if (WEB_DEMO_MODE) return; |
b0b4abcf | 157 | |
ab3d0b99 AD |
158 | $feed = db_escape_string($feed); |
159 | ||
3ad5aa85 | 160 | error_reporting(0); |
40d13c28 | 161 | $rss = fetch_rss($feed_url); |
ab3d0b99 | 162 | |
3ad5aa85 | 163 | error_reporting (E_ERROR | E_WARNING | E_PARSE); |
76798ff3 | 164 | |
b6eefba5 | 165 | db_query($link, "BEGIN"); |
b7f4bda2 | 166 | |
b6eefba5 | 167 | $feed = db_escape_string($feed); |
dcee8f61 | 168 | |
40d13c28 | 169 | if ($rss) { |
b82af8c3 | 170 | |
b619ff15 | 171 | if (get_pref($link, 'ENABLE_FEED_ICONS')) { |
9e997874 | 172 | check_feed_favicon($feed_url, $feed, $link); |
78800912 AD |
173 | } |
174 | ||
b6eefba5 | 175 | $result = db_query($link, "SELECT title,icon_url FROM ttrss_feeds WHERE id = '$feed'"); |
331900c6 | 176 | |
b6eefba5 AD |
177 | $registered_title = db_fetch_result($result, 0, "title"); |
178 | $orig_icon_url = db_fetch_result($result, 0, "icon_url"); | |
331900c6 AD |
179 | |
180 | if (!$registered_title) { | |
e1305a97 | 181 | $feed_title = db_escape_string($rss->channel["title"]); |
b6eefba5 | 182 | db_query($link, "UPDATE ttrss_feeds SET title = '$feed_title' WHERE id = '$feed'"); |
331900c6 | 183 | } |
40d13c28 | 184 | |
b7f4bda2 AD |
185 | // print "I: " . $rss->channel["image"]["url"]; |
186 | ||
187 | $icon_url = $rss->image["url"]; | |
188 | ||
189 | if ($icon_url && !$orig_icon_url) { | |
b6eefba5 AD |
190 | $icon_url = db_escape_string($icon_url); |
191 | db_query($link, "UPDATE ttrss_feeds SET icon_url = '$icon_url' WHERE id = '$feed'"); | |
b7f4bda2 AD |
192 | } |
193 | ||
e6155a06 AD |
194 | |
195 | $filters = array(); | |
196 | ||
4b3dff6e | 197 | $result = db_query($link, "SELECT reg_exp, |
e6155a06 AD |
198 | (SELECT name FROM ttrss_filter_types |
199 | WHERE id = filter_type) as name | |
06da450f | 200 | FROM ttrss_filters WHERE owner_uid = ".$_SESSION["uid"]); |
e6155a06 | 201 | |
b6eefba5 | 202 | while ($line = db_fetch_assoc($result)) { |
e6155a06 | 203 | if (!$filters[$line["name"]]) $filters[$line["name"]] = array(); |
4b3dff6e | 204 | array_push($filters[$line["name"]], $line["reg_exp"]); |
e6155a06 AD |
205 | } |
206 | ||
40d13c28 AD |
207 | foreach ($rss->items as $item) { |
208 | ||
209 | $entry_guid = $item["id"]; | |
210 | ||
211 | if (!$entry_guid) $entry_guid = $item["guid"]; | |
212 | if (!$entry_guid) $entry_guid = $item["link"]; | |
466001c4 AD |
213 | |
214 | if (!$entry_guid) continue; | |
a116f569 | 215 | |
9c9c7e6b | 216 | $entry_timestamp = ""; |
b82af8c3 | 217 | |
9c9c7e6b AD |
218 | $rss_2_date = $item['pubdate']; |
219 | $rss_1_date = $item['dc']['date']; | |
220 | $atom_date = $item['issued']; | |
b197f117 | 221 | |
9c9c7e6b AD |
222 | if ($atom_date != "") $entry_timestamp = parse_w3cdtf($atom_date); |
223 | if ($rss_1_date != "") $entry_timestamp = parse_w3cdtf($rss_1_date); | |
224 | if ($rss_2_date != "") $entry_timestamp = strtotime($rss_2_date); | |
b82af8c3 AD |
225 | |
226 | if ($entry_timestamp == "") { | |
227 | $entry_timestamp = time(); | |
228 | $no_orig_date = 'true'; | |
466001c4 AD |
229 | } else { |
230 | $no_orig_date = 'false'; | |
b82af8c3 | 231 | } |
b197f117 | 232 | |
466001c4 | 233 | $entry_timestamp_fmt = strftime("%Y/%m/%d %H:%M:%S", $entry_timestamp); |
71ad3959 | 234 | |
40d13c28 AD |
235 | $entry_title = $item["title"]; |
236 | $entry_link = $item["link"]; | |
71ad3959 AD |
237 | |
238 | if (!$entry_title) continue; | |
239 | if (!$entry_link) continue; | |
240 | ||
1696229f AD |
241 | $entry_content = $item["content:escaped"]; |
242 | ||
243 | if (!$entry_content) $entry_content = $item["content:encoded"]; | |
40d13c28 | 244 | if (!$entry_content) $entry_content = $item["content"]; |
1696229f | 245 | if (!$entry_content) $entry_content = $item["description"]; |
a2015351 | 246 | |
a116f569 | 247 | // if (!$entry_content) continue; |
a2015351 | 248 | |
8add756a AD |
249 | // WTF |
250 | if (is_array($entry_content)) { | |
251 | $entry_content = $entry_content["encoded"]; | |
1696229f | 252 | if (!$entry_content) $entry_content = $entry_content["escaped"]; |
8add756a AD |
253 | } |
254 | ||
1696229f AD |
255 | // print_r($item); |
256 | // print_r($entry_content); | |
257 | ||
466001c4 | 258 | $content_hash = "SHA1:" . sha1(strip_tags($entry_content)); |
cb0bd8bd | 259 | |
a1ea1e12 AD |
260 | $entry_comments = $item["comments"]; |
261 | ||
b6eefba5 | 262 | $entry_guid = db_escape_string($entry_guid); |
2651fc4f | 263 | |
4c193675 AD |
264 | $result = db_query($link, "SELECT id FROM ttrss_entries WHERE guid = '$entry_guid'"); |
265 | ||
266 | $owner_uid = $_SESSION["uid"]; | |
267 | ||
268 | if (db_num_rows($result) == 0) { | |
269 | ||
270 | // base post entry does not exist, create it | |
271 | ||
272 | error_reporting(0); | |
273 | if (is_filtered($entry_title, $entry_content, $filters)) { | |
274 | continue; | |
275 | } | |
276 | error_reporting (E_ERROR | E_WARNING | E_PARSE); | |
277 | ||
278 | $entry_content = db_escape_string($entry_content); | |
279 | $entry_title = db_escape_string($entry_title); | |
280 | $entry_link = db_escape_string($entry_link); | |
281 | $entry_comments = db_escape_string($entry_comments); | |
282 | ||
283 | $result = db_query($link, | |
284 | "INSERT INTO ttrss_entries | |
285 | (title, | |
286 | guid, | |
287 | link, | |
288 | updated, | |
289 | content, | |
290 | content_hash, | |
291 | no_orig_date, | |
292 | date_entered, | |
293 | comments) | |
294 | VALUES | |
295 | ('$entry_title', | |
296 | '$entry_guid', | |
297 | '$entry_link', | |
298 | '$entry_timestamp_fmt', | |
299 | '$entry_content', | |
300 | '$content_hash', | |
301 | $no_orig_date, | |
302 | NOW(), | |
303 | '$entry_comments')"); | |
304 | } | |
305 | ||
306 | // now it should exist, if not - bad luck then | |
307 | ||
308 | $result = db_query($link, "SELECT id FROM ttrss_entries WHERE guid = '$entry_guid'"); | |
309 | ||
310 | if (db_num_rows($result) == 1) { | |
311 | ||
312 | $ref_id = db_fetch_result($result, 0, "id"); | |
313 | ||
314 | // check for user post link to main table | |
315 | ||
316 | $result = db_query($link, | |
317 | "SELECT ref_id FROM ttrss_user_entries WHERE | |
318 | ref_id = '$ref_id' AND owner_uid = '$owner_uid' AND feed_id = '$feed'"); | |
319 | ||
320 | // okay it doesn't exist - create user entry | |
321 | ||
322 | if (db_num_rows($result) == 0) { | |
323 | ||
324 | $result = db_query($link, | |
325 | "INSERT INTO ttrss_user_entries | |
326 | (ref_id, owner_uid, feed_id) | |
327 | VALUES ('$ref_id', '$owner_uid', '$feed')"); | |
328 | ||
329 | } | |
330 | } | |
331 | ||
332 | /* $result = db_query($link, " | |
40d13c28 | 333 | SELECT |
ecb14114 AD |
334 | id,last_read,no_orig_date,title,feed_id,content_hash, |
335 | substring(updated,1,19) as updated | |
40d13c28 AD |
336 | FROM |
337 | ttrss_entries | |
338 | WHERE | |
4e90d6d2 AD |
339 | guid = '$entry_guid' AND |
340 | owner_uid = " . $_SESSION["uid"]." AND | |
341 | feed_id = '$feed'"); | |
466001c4 | 342 | |
b6eefba5 | 343 | if (db_num_rows($result) == 0) { |
466001c4 | 344 | |
e6155a06 AD |
345 | error_reporting(0); |
346 | if (is_filtered($entry_title, $entry_content, $filters)) { | |
347 | continue; | |
348 | } | |
349 | error_reporting (E_ERROR | E_WARNING | E_PARSE); | |
350 | ||
b6eefba5 AD |
351 | //$entry_guid = db_escape_string($entry_guid); |
352 | $entry_content = db_escape_string($entry_content); | |
353 | $entry_title = db_escape_string($entry_title); | |
354 | $entry_link = db_escape_string($entry_link); | |
355 | $entry_comments = db_escape_string($entry_comments); | |
466001c4 AD |
356 | |
357 | $query = "INSERT | |
358 | INTO ttrss_entries | |
359 | (title, | |
360 | guid, | |
361 | link, | |
362 | updated, | |
363 | content, | |
364 | content_hash, | |
365 | feed_id, | |
deaaa02c | 366 | comments, |
3b063a95 | 367 | no_orig_date, |
06da450f AD |
368 | date_entered, |
369 | owner_uid) | |
40d13c28 | 370 | VALUES |
466001c4 AD |
371 | ('$entry_title', |
372 | '$entry_guid', | |
373 | '$entry_link', | |
374 | '$entry_timestamp_fmt', | |
375 | '$entry_content', | |
376 | '$content_hash', | |
377 | '$feed', | |
a1ea1e12 | 378 | '$entry_comments', |
3b063a95 | 379 | $no_orig_date, |
06da450f | 380 | NOW(),".$_SESSION["uid"].")"; |
466001c4 | 381 | |
b6eefba5 | 382 | $result = db_query($link, $query); |
76798ff3 | 383 | |
40d13c28 | 384 | } else { |
466001c4 | 385 | |
b6eefba5 AD |
386 | $orig_entry_id = db_fetch_result($result, 0, "id"); |
387 | $orig_feed_id = db_fetch_result($result, 0, "feed_id"); | |
466001c4 | 388 | |
ecb14114 AD |
389 | // print "OED: $orig_entry_id; OID: $orig_feed_id ; FID: $feed<br>"; |
390 | ||
466001c4 | 391 | if ($orig_feed_id != $feed) { |
ecb14114 | 392 | // print "<p>GUID $entry_guid: update from different feed ($orig_feed_id, $feed): $entry_guid [$entry_title]"; |
466001c4 AD |
393 | continue; |
394 | } | |
ad3024fc AD |
395 | |
396 | $entry_is_modified = false; | |
466001c4 | 397 | |
ecb14114 AD |
398 | $orig_timestamp = strtotime(db_fetch_result($result, 0, "updated")); |
399 | ||
b6eefba5 AD |
400 | $orig_content_hash = db_fetch_result($result, 0, "content_hash"); |
401 | $orig_last_read = db_fetch_result($result, 0, "last_read"); | |
402 | $orig_no_orig_date = db_fetch_result($result, 0, "no_orig_date"); | |
403 | $orig_title = db_fetch_result($result, 0, "title"); | |
cac95b8d | 404 | |
2d84262b AD |
405 | $last_read_qpart = ""; |
406 | ||
ad3024fc | 407 | if ($orig_content_hash != $content_hash) { |
ecb14114 AD |
408 | // print "$orig_content_hash :: $content_hash<br>"; |
409 | ||
b619ff15 | 410 | if (get_pref($link, 'UPDATE_POST_ON_CHECKSUM_CHANGE')) { |
ad3024fc AD |
411 | $last_read_qpart = 'last_read = null,'; |
412 | } | |
413 | $entry_is_modified = true; | |
414 | } | |
415 | ||
416 | if ($orig_title != $entry_title) { | |
417 | $entry_is_modified = true; | |
418 | } | |
419 | ||
420 | if ($orig_timestamp != $entry_timestamp && !$orig_no_orig_date) { | |
421 | $entry_is_modified = true; | |
cac95b8d AD |
422 | } |
423 | ||
ad3024fc | 424 | if ($entry_is_modified) { |
a2015351 | 425 | |
ecb14114 AD |
426 | // print "$entry_guid Modified!<br>"; |
427 | ||
b6eefba5 AD |
428 | $entry_comments = db_escape_string($entry_comments); |
429 | $entry_content = db_escape_string($entry_content); | |
430 | $entry_title = db_escape_string($entry_title); | |
431 | $entry_link = db_escape_string($entry_link); | |
a2015351 | 432 | |
ad3024fc AD |
433 | $query = "UPDATE ttrss_entries |
434 | SET | |
435 | $last_read_qpart | |
436 | title = '$entry_title', | |
437 | link = '$entry_link', | |
438 | updated = '$entry_timestamp_fmt', | |
439 | content = '$entry_content', | |
440 | comments = '$entry_comments', | |
441 | content_hash = '$content_hash' | |
442 | WHERE | |
443 | id = '$orig_entry_id'"; | |
444 | ||
b6eefba5 | 445 | $result = db_query($link, $query); |
ad3024fc | 446 | } |
4c193675 | 447 | } */ |
40d13c28 | 448 | |
eb36b4eb AD |
449 | /* taaaags */ |
450 | // <a href="http://technorati.com/tag/Xorg" rel="tag">Xorg</a>, // | |
451 | ||
4c193675 | 452 | /* $entry_tags = null; |
eb36b4eb AD |
453 | |
454 | preg_match_all("/<a.*?rel=.tag.*?>([^>]+)<\/a>/i", $entry_content, | |
455 | $entry_tags); | |
456 | ||
457 | $entry_tags = $entry_tags[1]; | |
458 | ||
459 | if (count($entry_tags) > 0) { | |
460 | ||
461 | $result = db_query($link, "SELECT id FROM ttrss_entries | |
25da6909 AD |
462 | WHERE guid = '$entry_guid' |
463 | AND feed_id = '$feed' | |
464 | AND owner_uid = " . $_SESSION["uid"]); | |
eb36b4eb AD |
465 | |
466 | if (!$result || db_num_rows($result) != 1) { | |
467 | return; | |
468 | } | |
469 | ||
470 | $entry_id = db_fetch_result($result, 0, "id"); | |
ab15e65d | 471 | |
eb36b4eb AD |
472 | foreach ($entry_tags as $tag) { |
473 | $tag = db_escape_string(strtolower($tag)); | |
474 | ||
3b0948c4 AD |
475 | $tag = str_replace("technorati tag: ", "", $tag); |
476 | ||
eb36b4eb | 477 | $result = db_query($link, "SELECT id FROM ttrss_tags |
ab15e65d AD |
478 | WHERE tag_name = '$tag' AND post_id = '$entry_id' AND |
479 | owner_uid = ".$_SESSION["uid"]." LIMIT 1"); | |
480 | ||
481 | // print db_fetch_result($result, 0, "id"); | |
eb36b4eb AD |
482 | |
483 | if ($result && db_num_rows($result) == 0) { | |
484 | ||
485 | // print "tagging $entry_id as $tag<br>"; | |
486 | ||
06da450f AD |
487 | db_query($link, "INSERT INTO ttrss_tags (owner_uid,tag_name,post_id) |
488 | VALUES ('".$_SESSION["uid"]."','$tag', '$entry_id')"); | |
eb36b4eb AD |
489 | } |
490 | } | |
4c193675 AD |
491 | } */ |
492 | } | |
40d13c28 | 493 | |
ab3d0b99 AD |
494 | db_query($link, "UPDATE ttrss_feeds |
495 | SET last_updated = NOW(), last_error = '' WHERE id = '$feed'"); | |
eb36b4eb | 496 | |
ab3d0b99 AD |
497 | } else { |
498 | $error_msg = db_escape_string(magpie_error()); | |
499 | db_query($link, | |
aa5f9f5f AD |
500 | "UPDATE ttrss_feeds SET last_error = '$error_msg', |
501 | last_updated = NOW() WHERE id = '$feed'"); | |
40d13c28 AD |
502 | } |
503 | ||
b6eefba5 | 504 | db_query($link, "COMMIT"); |
f48ba3c9 | 505 | |
40d13c28 AD |
506 | } |
507 | ||
f175937c AD |
508 | function print_select($id, $default, $values, $attributes = "") { |
509 | print "<select id=\"$id\" $attributes>"; | |
a0d53889 AD |
510 | foreach ($values as $v) { |
511 | if ($v == $default) | |
512 | $sel = " selected"; | |
513 | else | |
514 | $sel = ""; | |
515 | ||
516 | print "<option$sel>$v</option>"; | |
517 | } | |
518 | print "</select>"; | |
519 | } | |
40d13c28 | 520 | |
e6155a06 AD |
521 | function is_filtered($title, $content, $filters) { |
522 | ||
523 | if ($filters["title"]) { | |
524 | foreach ($filters["title"] as $title_filter) { | |
525 | if (preg_match("/$title_filter/i", $title)) | |
526 | return true; | |
527 | } | |
528 | } | |
529 | ||
530 | if ($filters["content"]) { | |
531 | foreach ($filters["content"] as $content_filter) { | |
532 | if (preg_match("/$content_filter/i", $content)) | |
533 | return true; | |
534 | } | |
535 | } | |
536 | ||
537 | if ($filters["both"]) { | |
538 | foreach ($filters["both"] as $filter) { | |
539 | if (preg_match("/$filter/i", $title) || preg_match("/$filter/i", $content)) | |
540 | return true; | |
541 | } | |
542 | } | |
543 | ||
544 | return false; | |
545 | } | |
546 | ||
4668523d | 547 | function printFeedEntry($feed_id, $class, $feed_title, $unread, $icon_file, $link) { |
254e0e4b AD |
548 | |
549 | if (file_exists($icon_file) && filesize($icon_file) > 0) { | |
550 | $feed_icon = "<img src=\"$icon_file\">"; | |
551 | } else { | |
552 | $feed_icon = "<img src=\"images/blank_icon.gif\">"; | |
553 | } | |
554 | ||
8143ae1f | 555 | $feed = "<a href=\"javascript:viewfeed('$feed_id', 0);\">$feed_title</a>"; |
254e0e4b AD |
556 | |
557 | print "<li id=\"FEEDR-$feed_id\" class=\"$class\">"; | |
b619ff15 | 558 | if (get_pref($link, 'ENABLE_FEED_ICONS')) { |
254e0e4b AD |
559 | print "$feed_icon"; |
560 | } | |
561 | ||
562 | print "<span id=\"FEEDN-$feed_id\">$feed</span>"; | |
563 | ||
564 | if ($unread != 0) { | |
565 | $fctr_class = ""; | |
566 | } else { | |
567 | $fctr_class = "class=\"invisible\""; | |
568 | } | |
569 | ||
570 | print "<span $fctr_class id=\"FEEDCTR-$feed_id\"> | |
571 | (<span id=\"FEEDU-$feed_id\">$unread</span>)</span>"; | |
572 | ||
573 | print "</li>"; | |
574 | ||
575 | } | |
576 | ||
406d9489 AD |
577 | function getmicrotime() { |
578 | list($usec, $sec) = explode(" ",microtime()); | |
579 | return ((float)$usec + (float)$sec); | |
580 | } | |
581 | ||
77e96719 AD |
582 | function print_radio($id, $default, $values, $attributes = "") { |
583 | foreach ($values as $v) { | |
584 | ||
585 | if ($v == $default) | |
5da169d9 | 586 | $sel = "checked"; |
77e96719 | 587 | else |
5da169d9 AD |
588 | $sel = ""; |
589 | ||
590 | if ($v == "Yes") { | |
591 | $sel .= " value=\"1\""; | |
592 | } else { | |
593 | $sel .= " value=\"0\""; | |
594 | } | |
77e96719 AD |
595 | |
596 | print "<input type=\"radio\" $sel $attributes name=\"$id\"> $v "; | |
597 | ||
598 | } | |
599 | } | |
600 | ||
ff485f1d AD |
601 | function initialize_user_prefs($link, $uid) { |
602 | ||
603 | $uid = db_escape_string($uid); | |
604 | ||
605 | db_query($link, "BEGIN"); | |
606 | ||
607 | $result = db_query($link, "SELECT pref_name,def_value FROM ttrss_prefs"); | |
608 | ||
609 | $u_result = db_query($link, "SELECT pref_name | |
610 | FROM ttrss_user_prefs WHERE owner_uid = '$uid'"); | |
611 | ||
612 | $active_prefs = array(); | |
613 | ||
614 | while ($line = db_fetch_assoc($u_result)) { | |
615 | array_push($active_prefs, $line["pref_name"]); | |
616 | } | |
617 | ||
618 | while ($line = db_fetch_assoc($result)) { | |
619 | if (array_search($line["pref_name"], $active_prefs) === FALSE) { | |
620 | // print "adding " . $line["pref_name"] . "<br>"; | |
621 | ||
622 | db_query($link, "INSERT INTO ttrss_user_prefs | |
623 | (owner_uid,pref_name,value) VALUES | |
624 | ('$uid', '".$line["pref_name"]."','".$line["def_value"]."')"); | |
625 | ||
626 | } | |
627 | } | |
628 | ||
629 | db_query($link, "COMMIT"); | |
630 | ||
631 | } | |
c8437f35 AD |
632 | |
633 | function authenticate_user($link, $login, $password) { | |
634 | ||
635 | $pwd_hash = 'SHA1:' . sha1($password); | |
636 | ||
203b6d25 | 637 | $result = db_query($link, "SELECT id,login,access_level FROM ttrss_users WHERE |
c8437f35 AD |
638 | login = '$login' AND (pwd_hash = '$password' OR pwd_hash = '$pwd_hash')"); |
639 | ||
640 | if (db_num_rows($result) == 1) { | |
641 | $_SESSION["uid"] = db_fetch_result($result, 0, "id"); | |
642 | $_SESSION["name"] = db_fetch_result($result, 0, "login"); | |
203b6d25 | 643 | $_SESSION["access_level"] = db_fetch_result($result, 0, "access_level"); |
c8437f35 | 644 | |
f6f32198 AD |
645 | db_query($link, "UPDATE ttrss_users SET last_login = NOW() WHERE id = " . |
646 | $_SESSION["uid"]); | |
647 | ||
c8437f35 AD |
648 | return true; |
649 | } | |
ff485f1d | 650 | |
c8437f35 AD |
651 | return false; |
652 | ||
653 | } | |
654 | ||
655 | function http_authenticate_user($link) { | |
1c7f75ed AD |
656 | |
657 | if (!$_SERVER['PHP_AUTH_USER']) { | |
658 | ||
659 | header('WWW-Authenticate: Basic realm="Tiny Tiny RSS"'); | |
660 | header('HTTP/1.0 401 Unauthorized'); | |
661 | print "<h1>401 Unathorized</h1>"; | |
662 | exit; | |
663 | ||
664 | } else { | |
665 | ||
666 | $login = db_escape_string($_SERVER['PHP_AUTH_USER']); | |
667 | $password = db_escape_string($_SERVER['PHP_AUTH_PW']); | |
1c7f75ed | 668 | |
c8437f35 AD |
669 | return authenticate_user($link, $login, $password); |
670 | } | |
1c7f75ed AD |
671 | } |
672 | ||
e6cb77a0 AD |
673 | function make_password($length = 8) { |
674 | ||
675 | $password = ""; | |
676 | $possible = "0123456789bcdfghjkmnpqrstvwxyz"; | |
677 | ||
678 | $i = 0; | |
679 | ||
680 | while ($i < $length) { | |
681 | $char = substr($possible, mt_rand(0, strlen($possible)-1), 1); | |
682 | ||
683 | if (!strstr($password, $char)) { | |
684 | $password .= $char; | |
685 | $i++; | |
686 | } | |
687 | } | |
688 | return $password; | |
689 | } | |
690 | ||
691 | // this is called after user is created to initialize default feeds, labels | |
692 | // or whatever else | |
693 | ||
694 | // user preferences are checked on every login, not here | |
695 | ||
696 | function initialize_user($link, $uid) { | |
697 | ||
698 | db_query($link, "insert into ttrss_labels (owner_uid,sql_exp,description) | |
699 | values ('$uid','unread = true', 'Unread articles')"); | |
700 | ||
701 | db_query($link, "insert into ttrss_labels (owner_uid,sql_exp,description) | |
702 | values ('$uid','last_read is null and unread = false', 'Updated articles')"); | |
703 | ||
704 | db_query($link, "insert into ttrss_feeds (owner_uid,title,feed_url) | |
705 | values ('$uid', 'Tiny Tiny RSS Dev. Feed', | |
706 | 'http://bah.spb.su/darcsweb/darcsweb.cgi?r=tt-rss;a=rss')"); | |
707 | ||
708 | } | |
709 | ||
40d13c28 | 710 | ?> |