+ $rm = new Remember_me();
+ $rm->code = common_good_rand(16);
+ $rm->user_id = $user->id;
+ $result = $rm->insert();
+ if (!$result) {
+ common_log_db_error($rm, 'INSERT', __FILE__);
+ common_debug('Error adding rememberme record for ' . $user->nickname, __FILE__);
+ return false;
+ }
+ common_log(LOG_INFO, 'adding rememberme cookie for ' . $user->nickname);
+ common_set_cookie(REMEMBERME,
+ implode(':', array($rm->user_id, $rm->code)),
+ time() + REMEMBERME_EXPIRY);
+ return true;
+}
+
+function common_remembered_user() {
+ $user = NULL;
+ # Try to remember
+ $packed = $_COOKIE[REMEMBERME];
+ if ($packed) {
+ list($id, $code) = explode(':', $packed);
+ if ($id && $code) {
+ $rm = Remember_me::staticGet($code);
+ if ($rm && ($rm->user_id == $id)) {
+ $user = User::staticGet($rm->user_id);
+ if ($user) {
+ # successful!
+ $result = $rm->delete();
+ if (!$result) {
+ common_log_db_error($rm, 'DELETE', __FILE__);
+ $user = NULL;
+ } else {
+ common_log(LOG_INFO, 'logging in ' . $user->nickname . ' using rememberme code ' . $rm->code);
+ common_set_user($user->nickname);
+ common_real_login(false);
+ # We issue a new cookie, so they can log in
+ # automatically again after this session
+ common_rememberme($user);
+ }
+ }
+ }
+ }
+ }
+ return $user;
+}
+
+# must be called with a valid user!
+
+function common_forgetme() {
+ common_set_cookie(REMEMBERME, '', 0);