+function login_setcookie()
+{
+ global $User;
+ return setcookie('login', $User->auth, 0, '/');
+}
+
+function login($inuser, $inpass = NULL)
+{
+ if (empty($inuser)) return;
+ if (!isset($inpass)) {
+ @list ($inuser, $inauth) = explode(':', $inuser, 2);
+ }
+
+ # find password data by user name
+ $userdir = 'profile/'.preg_replace('/[^a-z0-9]+/', '-', strtolower($inuser));
+ $pwfile = "$userdir/.passwd";
+ if (!file_exists($pwfile)) return;
+ $usertest = trim(file_get_contents($pwfile));
+ if (!$usertest) return;
+
+ # verify password
+ $authhash = md5($usertest);
+ if (isset($inpass)) {
+ if (!login_password_verify($inpass, $usertest)) return;
+ }
+ else {
+ if ($inauth !== $authhash) return;
+ }
+
+ if (function_exists('apache_note')) apache_note('user', $inuser);
+
+ $user = new User($userdir);
+ $user->logclient();
+ $user->pass = $usertest;
+ $user->auth = "$inuser:$authhash";
+ return $user;
+}
+
+global $User;
+if (isset($_COOKIE['login'])) {
+ $User = login($_COOKIE['login']);
+}
+if (!$User) {
+ $User = new User;
+}