Example #1
0
 public static function init()
 {
     self::setIP();
     self::setLocale();
     // session have a dataKey to access the JScripts (yes, also the anons)
     if (empty($_SESSION['dataKey'])) {
         $_SESSION['dataKey'] = Util::createHash();
     }
     // just some random numbers for identifictaion purpose
     self::$dataKey = $_SESSION['dataKey'];
     if (!self::$ip) {
         return false;
     }
     // check IP bans
     if ($ipBan = DB::Aowow()->selectRow('SELECT count, unbanDate FROM ?_account_bannedips WHERE ip = ? AND type = 0', self::$ip)) {
         if ($ipBan['count'] > CFG_FAILED_AUTH_COUNT && $ipBan['unbanDate'] > time()) {
             return false;
         } else {
             if ($ipBan['unbanDate'] <= time()) {
                 DB::Aowow()->query('DELETE FROM ?_account_bannedips WHERE ip = ?', self::$ip);
             }
         }
     }
     // try to restore session
     if (empty($_SESSION['user'])) {
         return false;
     }
     // timed out...
     if (!empty($_SESSION['timeout']) && $_SESSION['timeout'] <= time()) {
         return false;
     }
     $query = DB::Aowow()->SelectRow('
         SELECT    a.id, a.passHash, a.displayName, a.locale, a.userGroups, a.userPerms, a.allowExpire, BIT_OR(ab.typeMask) AS bans, IFNULL(SUM(r.amount), 0) as reputation, a.avatar, a.dailyVotes
         FROM      ?_account a
         LEFT JOIN ?_account_banned ab ON a.id = ab.userId AND ab.end > UNIX_TIMESTAMP()
         LEFT JOIN ?_account_reputation r ON a.id = r.userId
         WHERE     a.id = ?d
         GROUP     BY a.id', $_SESSION['user']);
     if (!$query) {
         return false;
     }
     // password changed, terminate session
     if (AUTH_MODE_SELF && $query['passHash'] != $_SESSION['hash']) {
         self::destroy();
         return false;
     }
     self::$id = intval($query['id']);
     self::$displayName = $query['displayName'];
     self::$passHash = $query['passHash'];
     self::$expires = (bool) $query['allowExpire'];
     self::$reputation = $query['reputation'];
     self::$banStatus = $query['bans'];
     self::$groups = $query['bans'] & (ACC_BAN_TEMP | ACC_BAN_PERM) ? 0 : intval($query['userGroups']);
     self::$perms = $query['bans'] & (ACC_BAN_TEMP | ACC_BAN_PERM) ? 0 : intval($query['userPerms']);
     self::$dailyVotes = $query['dailyVotes'];
     if ($query['avatar']) {
         self::$avatar = $query['avatar'];
     }
     if (self::$localeId != $query['locale']) {
         // reset, if changed
         self::setLocale(intVal($query['locale']));
     }
     // stuff, that updates on a daily basis goes here (if you keep you session alive indefinitly, the signin-handler doesn't do very much)
     // - conscutive visits
     // - votes per day
     // - reputation for daily visit
     if (self::$id) {
         $lastLogin = DB::Aowow()->selectCell('SELECT curLogin FROM ?_account WHERE id = ?d', self::$id);
         // either the day changed or the last visit was >24h ago
         if (date('j', $lastLogin) != date('j') || time() - $lastLogin > 1 * DAY) {
             // daily votes (we need to reset this one)
             self::$dailyVotes = self::getMaxDailyVotes();
             DB::Aowow()->query('
                 UPDATE  ?_account
                 SET     dailyVotes = ?d, prevLogin = curLogin, curLogin = UNIX_TIMESTAMP(), prevIP = curIP, curIP = ?
                 WHERE   id = ?d', self::$dailyVotes, self::$ip, self::$id);
             // gain rep for daily visit
             if (!(self::$banStatus & (ACC_BAN_TEMP | ACC_BAN_PERM))) {
                 Util::gainSiteReputation(self::$id, SITEREP_ACTION_DAILYVISIT);
             }
             // increment consecutive visits (next day or first of new month and not more than 48h)
             // i bet my ass i forgott a corner case
             if ((date('j', $lastLogin) + 1 == date('j') || date('j') == 1 && date('n', $lastLogin) != date('n')) && time() - $lastLogin < 2 * DAY) {
                 DB::Aowow()->query('UPDATE ?_account SET consecutiveVisits = consecutiveVisits + 1 WHERE id = ?d', self::$id);
             } else {
                 DB::Aowow()->query('UPDATE ?_account SET consecutiveVisits = 0 WHERE id = ?d', self::$id);
             }
         }
     }
     return true;
 }