_uid = 0; $this -> _validIPs = array(); $this -> _level = RSS_USER_LEVEL_NOLEVEL; $this -> _uname = ''; $this -> _realName = ''; $this -> _hash = null; $this -> _showPrivate = 0; $this -> _mobileSession = isset($_POST['media']) && 'mobile' == $_POST['media']; if ('mobile' == getThemeMedia()) { @ini_set('session.use_trans_sid',true); session_start(); } if (array_key_exists('logout',$_GET)) { $this -> logout(); rss_redirect(''); } $cuname = $chash = null; if (isset($_POST['username']) && isset($_POST['password'])) { $_cuname = trim($_POST['username']); if ($this -> _mobileSession) { $_chash = md5(md5($_POST['password'] . $_POST['username'])); } else { $_chash = md5($_POST['password']); } if ($this -> login($_cuname,$_chash)) { $cuname = $_cuname; $chash = $_chash; $this -> _action = RSS_USER_ACTION_LOGIN; } } elseif (isset($_COOKIE[RSS_USER_COOKIE])) { list($cuname,$chash) = explode('|',$_COOKIE[RSS_USER_COOKIE]); $this -> _action = RSS_USER_ACTION_COOKIE; } elseif(isset($_SESSION['mobile'])) { list($cuname,$chash) = explode('|',$_SESSION['mobile']); $this -> _mobileSession = true; $this -> _action = RSS_USER_ACTION_SESSION; } if ($cuname && $chash) { $sql = "select uid, uname, ulevel, realname, userips from " . getTable('users') . " where uname='" .rss_real_escape_string($cuname) ."' and password='" .preg_replace('#[^a-zA-Z0-9]#','',md5($chash)) ."'"; $rs = rss_query($sql); if (rss_num_rows($rs) == 1) { list($uid, $uname, $level, $realName, $tmpUserIps) = rss_fetch_row($rs); $userIPs = explode(' ',$tmpUserIps); $subnet = preg_replace('#^([0-9]+\.[0-9]+\.[0-9]+)\.[0-9]+$#','\1',$_SERVER['REMOTE_ADDR']); if ((array_search($subnet, $userIPs) !== FALSE) || ($this -> _action != RSS_USER_ACTION_COOKIE)) { $this -> _uid = $uid; $this -> _uname = $uname; $this -> _validIPs = $userIPs; $this -> _level = $level; $this -> _realName = $realName; $this -> _hash = $chash; } } } } /** * Logs in a user given the username and password. * If the user provided valid username and password, * he is given a cookie and his IP address subnet is added * to the list of valid IPs this user is allowed to log in * via a cookie * * Returns true on a successful login, false otherwise. */ function login($uname,$pass) { $sql ="select uname,ulevel,userips from " .getTable('users') . "where uname='" .rss_real_escape_string($uname)."' and password='".md5($pass)."'"; list($uname,$ulevel,$userips) = rss_fetch_row(rss_query($sql)); if ($ulevel == '') { $ulevel = RSS_USER_LEVEL_NOLEVEL; return false; } else { // "push" the user IP into the list of logged-in IP subnets $subnet = preg_replace('#^([0-9]+\.[0-9]+\.[0-9]+)\.[0-9]+$#','\1',$_SERVER['REMOTE_ADDR']); $this -> _validIPs = explode(' ',$userips); $this -> _validIPs[] = $subnet; $sql = "update " .getTable('users') . " set userips = '" . implode(' ', $this -> _validIPs ) ."'" ." where uname = '$uname' "; rss_query($sql); if ($this -> _mobileSession) { $this -> setUserSession($uname,$pass); } else { $this -> setUserCookie($uname,$pass); } rss_invalidate_cache(); return true; } return false; } /** * Hands the user a yummy cookie. * The cookie holds the md5 hash of the user password */ function setUserCookie($user,$hash) { $rs = rss_query( 'select value_ from ' .getTable('config') . "where key_ = 'rss.config.autologout'", false,true); if (rss_is_sql_error(RSS_SQL_ERROR_NO_ERROR) && rss_num_rows($rs) > 0) { list($als) = rss_fetch_row($rs); $al = ($als == 'true'); } else { $al = false; } $t = $al ? 0: time()+COOKIE_LIFESPAN; setcookie(RSS_USER_COOKIE, $user .'|' . $hash , $t, getPath()); } function setUserSession($user,$hash) { $_SESSION['mobile'] = $user . "|" . $hash; } /** * Logs the user out. * - deletes the cookie * - removes the user's IP subnet from the list of valid subnets this * user is allowed to log in with a cookie. */ function logout() { if (array_key_exists(RSS_USER_COOKIE, $_COOKIE) || isset($_SESSION['mobile'])) { $subnet = preg_replace('#^([0-9]+\.[0-9]+\.[0-9]+)\.[0-9]+$#','\1',$_SERVER['REMOTE_ADDR']); if (($idx = array_search($subnet, $this -> _validIPs)) !== FALSE) { $cnt = count($this -> _validIPs); unset($this -> _validIPs[$idx]); $uname = trim($this -> _uname); if ($uname && ($cnt > count($this -> _validIPs))) { $sql = "update " .getTable('users') . " set userips = '" . implode(' ',$this -> _validIPs) ."'" ." where uname = '$uname' "; rss_query($sql); } } // get rid of the cookie unset($_COOKIE[RSS_USER_COOKIE]); setcookie(RSS_USER_COOKIE, "", -1, getPath()); if (isset($_SESSION['mobile'])) { unset($_SESSION['mobile']); } rss_invalidate_cache(); } } ///// Getters ////// function getUserName() { return $this -> _uname; } function getUserLevel() { return $this -> _level; } function getShowPrivate() { return true; //$this -> _showPrivate; } function setShowPrivate($show) { $this -> _showPrivate = $show; } } // Create the unique instance. $GLOBALS['rssuser'] = new RSSUser(); ?>