[289] | 1 | <?php |
---|
| 2 | class onlineUser { |
---|
| 3 | |
---|
| 4 | protected static $instance = null; |
---|
| 5 | |
---|
| 6 | public static function getInstance() { |
---|
| 7 | if (self::$instance==null) { |
---|
| 8 | self::$instance = new onlineUser(); |
---|
| 9 | } |
---|
| 10 | return self::$instance; |
---|
| 11 | } |
---|
| 12 | |
---|
| 13 | public function addUser(sfMyUser $user) { |
---|
| 14 | if (sfDatabaseSessionStorage::$is_bot) return; |
---|
| 15 | $this->user = $user; |
---|
| 16 | $pdo = sfDatabaseSessionStorage::$pdo; |
---|
| 17 | $blog = blogStore::$blogId; |
---|
| 18 | $time = time(); |
---|
| 19 | |
---|
| 20 | if ($user->isAuthenticated()) { |
---|
| 21 | $id = $pdo->quote($user->getUserId()); |
---|
| 22 | $fullname = $pdo->quote($user->getUserFullname()); |
---|
| 23 | $pdo->exec("insert into online_user values($blog, $id, $fullname, $time) on duplicate key update ou_time=$time"); |
---|
| 24 | } else { |
---|
| 25 | $crc = crc32(myUtility::getRealIpAddr()); |
---|
| 26 | $pdo->exec("insert into online_guest values($blog, $crc, $time) on duplicate key update og_time=$time"); |
---|
| 27 | } |
---|
| 28 | } |
---|
| 29 | |
---|
| 30 | public function removeCurrentUser() { |
---|
| 31 | if ($this->user) { |
---|
| 32 | if ($this->user->isAuthenticated()) { |
---|
| 33 | sfDatabaseSessionStorage::$pdo->exec('delete from online_user where ou_userid = '.$this->user->getUserId()); |
---|
| 34 | } else { |
---|
| 35 | $crc = crc32(myUtility::getRealIpAddr()); |
---|
| 36 | sfDatabaseSessionStorage::$pdo->exec('delete from online_guest where og_crc = '.$crc); |
---|
| 37 | } |
---|
| 38 | } |
---|
| 39 | } |
---|
| 40 | |
---|
| 41 | public function getOnlineUsers($baseLink, &$numGuest, &$numUser, $maxNum=100) { |
---|
| 42 | $ret = ''; |
---|
| 43 | $bl_id = blogStore::$blogId; |
---|
| 44 | $pdo = sfDatabaseSessionStorage::$pdo; |
---|
| 45 | $numGuest = $pdo->query("select count(*) from online_guest where og_blog=$bl_id")->fetchColumn(); |
---|
| 46 | $numUser = $pdo->query("select count(*) from online_user where ou_blog=$bl_id")->fetchColumn(); |
---|
| 47 | $stmt = $pdo->query("select ou_userid, ou_user_fullname from online_user where ou_blog=$bl_id limit $maxNum"); |
---|
| 48 | while ($row = $stmt->fetch(PDO::FETCH_NUM)) { |
---|
| 49 | $ret .= '<li>'.link_to($row[1], $baseLink.$row[0]).'</li>'; |
---|
| 50 | } |
---|
| 51 | return $ret; |
---|
| 52 | } |
---|
| 53 | |
---|
| 54 | public function getAllOnlineUsers($baseLink, &$numGuest, &$numUser, $maxNum=100) { |
---|
| 55 | $ret = ''; |
---|
| 56 | $bl_id = blogStore::$blogId; |
---|
| 57 | $pdo = sfDatabaseSessionStorage::$pdo; |
---|
| 58 | $numGuest = $pdo->query("select count(*) from sessions where ss_userid=0")->fetchColumn(); |
---|
| 59 | $numUser = $pdo->query("select count(*) from sessions where ss_userid>0")->fetchColumn(); |
---|
| 60 | $stmt = $pdo->query("select ss_userid, ss_user_fullname from sessions where ss_userid>0 limit $maxNum"); |
---|
| 61 | $order = 0; |
---|
| 62 | while ($row = $stmt->fetch(PDO::FETCH_NUM)) { |
---|
| 63 | if (++$order == 10) $ret .="<div id='user_online' style='display:none; cursor:pointer'>"; |
---|
| 64 | $ret .= '<li>'.link_to($row[1], $baseLink.$row[0]).'</li>'; |
---|
| 65 | } |
---|
| 66 | if ($numUser>=10) { |
---|
| 67 | $ret .="</div><span id='user_off' style=\"display:none; cursor:pointer\" onClick=\"document.getElementById('user_online').style.display='none';document.getElementById('user_on').style.display='inline'; document.getElementById('user_off').style.display='none'; \" > << </span> <span style=\"cursor:pointer\" id='user_on' onClick=\"document.getElementById('user_online').style.display='inline'; document.getElementById('user_on').style.display='none';document.getElementById('user_off').style.display='inline'; \" > >> </span>"; |
---|
| 68 | } |
---|
| 69 | return $ret; |
---|
| 70 | } |
---|
| 71 | } |
---|