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