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