[289] | 1 | <?php |
---|
| 2 | |
---|
| 3 | class sfMyUser extends sfBasicSecurityUser |
---|
| 4 | { |
---|
| 5 | public function initialize($context, $parameters = null) |
---|
| 6 | { |
---|
| 7 | parent::initialize($context, $parameters); |
---|
| 8 | |
---|
| 9 | $storage = $this->getContext()->getStorage(); |
---|
| 10 | $time = $storage->read(parent::LAST_REQUEST_NAMESPACE); |
---|
| 11 | if ($time == null && !$this->isAuthenticated()) $this->autoLogin(); |
---|
| 12 | |
---|
| 13 | if ($this->isAuthenticated()) { |
---|
| 14 | $ip = myUtility::getRealIpAddr(); |
---|
| 15 | $storeip = $this->getAttribute('userip', null, 'user'); |
---|
| 16 | if ($storeip == null) $this->setAttribute('userip', $ip, 'user'); |
---|
| 17 | else if ($storeip != $ip) { |
---|
| 18 | $this->setAuthenticated(false); |
---|
| 19 | session_regenerate_id(true); |
---|
| 20 | } |
---|
| 21 | } |
---|
| 22 | } |
---|
| 23 | |
---|
| 24 | public function rememberLogin($user, $remember = false) { |
---|
| 25 | $value = $remember? base64_encode(serialize(array($user->getUsKey(), $user->getUsId()))): ''; |
---|
| 26 | sfContext::getInstance()->getResponse()->setCookie('tvtl', $value, time() + 86400*15, '/'); |
---|
| 27 | } |
---|
| 28 | |
---|
| 29 | public function autoLogin() { |
---|
| 30 | $remember = sfContext::getInstance()->getRequest()->getCookie('tvtl'); |
---|
| 31 | if (!$remember) return; |
---|
| 32 | $info = unserialize(base64_decode($remember)); |
---|
| 33 | if (is_array($info) && count($info)==2) { |
---|
| 34 | $c = new Criteria(); |
---|
| 35 | $c->add(TbluserPeer::US_KEY, $info[0]); |
---|
| 36 | $c->add(TbluserPeer::US_ID, $info[1]); |
---|
| 37 | $tbluser = TbluserPeer::doSelectOne($c); |
---|
| 38 | try { |
---|
| 39 | if ($tbluser!=null) $this->signIn($tbluser); |
---|
| 40 | } catch (Exception $e) {} |
---|
| 41 | } |
---|
| 42 | } |
---|
| 43 | |
---|
| 44 | public function signIn($user) { |
---|
| 45 | $user->getObject()->checkUserLock(); |
---|
| 46 | if (class_exists('sfDatabaseSessionStorage', false)) sfDatabaseSessionStorage::kickUserByName($user->getUsUsername()); |
---|
| 47 | $this->setAuthenticated(true); |
---|
| 48 | $this->setAttribute('userid', $user->getUsId(), 'user'); |
---|
| 49 | $this->setAttribute('username', $user->getUsUsername(), 'user'); |
---|
| 50 | $this->setAttribute('userfullname', $user->getUsFullname(), 'user'); |
---|
| 51 | $this->setAttribute('usertitle', $user->getObject()->getTitle(), 'user'); |
---|
| 52 | $this->setAttribute('aclgroup', $user->getUsAclgroup(), 'user'); |
---|
| 53 | $this->setAttribute('password', $user->getUsPassword(), 'user'); |
---|
| 54 | |
---|
| 55 | if ($user->getUsAclgroup()=='1' || $user->getUsAclgroup()=='2') { |
---|
| 56 | $this->addCredential('moderator'); |
---|
| 57 | } |
---|
| 58 | $this->addCredential('user'); |
---|
| 59 | $user->getObject()->onLogin(); |
---|
| 60 | } |
---|
| 61 | |
---|
| 62 | public function signOut() |
---|
| 63 | { |
---|
| 64 | onlineUser::getInstance()->removeCurrentUser(); |
---|
| 65 | $this->getAttributeHolder()->removeNamespace('user'); |
---|
| 66 | |
---|
| 67 | $this->clearVote(); |
---|
| 68 | $this->clearBlogmember(); |
---|
| 69 | $this->setAuthenticated(false); |
---|
| 70 | $this->clearCredentials(); |
---|
| 71 | } |
---|
| 72 | |
---|
| 73 | public function isAdministrator() |
---|
| 74 | { |
---|
| 75 | return $this->isAuthenticated() && $this->getAttribute('aclgroup', '', 'user') == '1'; |
---|
| 76 | } |
---|
| 77 | |
---|
| 78 | public function getUserId() { |
---|
| 79 | if ($this->isAuthenticated()) return $this->getAttribute('userid', '', 'user'); |
---|
| 80 | else return 0; |
---|
| 81 | } |
---|
| 82 | |
---|
| 83 | public function getUserName() { |
---|
| 84 | if ($this->isAuthenticated()) return $this->getAttribute('username', '', 'user'); |
---|
| 85 | else return ''; |
---|
| 86 | } |
---|
| 87 | |
---|
| 88 | public function getUserFullname() { |
---|
| 89 | if ($this->isAuthenticated()) return $this->getAttribute('userfullname', '', 'user'); |
---|
| 90 | else return ''; |
---|
| 91 | } |
---|
| 92 | |
---|
| 93 | public function getUserTitle() { |
---|
| 94 | if ($this->isAuthenticated()) return $this->getAttribute('usertitle', '', 'user'); |
---|
| 95 | else return ''; |
---|
| 96 | } |
---|
| 97 | |
---|
| 98 | /** |
---|
| 99 | * return type of sort of current user (date/download/rate) |
---|
| 100 | */ |
---|
| 101 | public function getUserSort() { |
---|
| 102 | $request = sfContext::getInstance()->getRequest(); |
---|
| 103 | $sort = $request->getCookie('sort'); |
---|
| 104 | $sort = $request->getParameter('sort'); |
---|
| 105 | if ($sort==null) $sort = $request->getCookie('sort', 'date'); |
---|
| 106 | else sfContext::getInstance()->getResponse()->setCookie('sort', $sort); |
---|
| 107 | return $sort; |
---|
| 108 | } |
---|
| 109 | |
---|
| 110 | public function setUserSort($sort) { |
---|
| 111 | if ($sort == '') { |
---|
| 112 | $sort = $this->getRequest()->getCookie('sort'); |
---|
| 113 | if ($sort != null) return; |
---|
| 114 | $sort = 'date'; |
---|
| 115 | } |
---|
| 116 | sfContext::getInstance()->getResponse()->setCookie('sort', $sort, time()+60*60*24*15, '/'); |
---|
| 117 | } |
---|
| 118 | |
---|
| 119 | public function setIsRegistering($referer) { |
---|
| 120 | if ($referer==null) { |
---|
| 121 | $referer = sfContext::getRequest()->getReferer(); |
---|
| 122 | } |
---|
| 123 | return $this->setAttribute('regrefer', $referer, 'user'); |
---|
| 124 | } |
---|
| 125 | |
---|
| 126 | public function getIsRegistering() { |
---|
| 127 | return null!=$this->getAttribute('regrefer', null, 'user'); |
---|
| 128 | } |
---|
| 129 | |
---|
| 130 | public function getRegisterReferer() { |
---|
| 131 | return $this->getAttribute('regrefer', null, 'user'); |
---|
| 132 | } |
---|
| 133 | |
---|
| 134 | public function hasCredential($credentials, $useAnd = true) |
---|
| 135 | { |
---|
| 136 | $ret = true; |
---|
| 137 | if ((is_array($credentials) && $i = in_array('blogadmin', $credentials)) || $credentials=='blogadmin') { |
---|
| 138 | $ret = $this->isAdministrator() || (blogStore::$userId == blogStore::$blog->getBlUser()); |
---|
| 139 | if (@$i) unset($credentials[$i]); else $credentials = array(); |
---|
| 140 | } |
---|
| 141 | return $ret && sfBasicSecurityUser::hasCredential($credentials, $useAnd); |
---|
| 142 | } |
---|
| 143 | |
---|
| 144 | /** |
---|
| 145 | * return true if this user is member of blog |
---|
| 146 | * |
---|
| 147 | * @return bool |
---|
| 148 | */ |
---|
| 149 | public function isBlogMember() { |
---|
| 150 | if (!class_exists('blogStore')) return false; |
---|
| 151 | if (!$this->isAuthenticated()) return false; |
---|
| 152 | $isMember = $this->getAttribute('ismember'.blogStore::$blogId); |
---|
| 153 | if ($isMember===null) { |
---|
| 154 | $tblbloguser = TblbloguserPeer::retrieveByPK(blogStore::$blogId, blogStore::$userId); |
---|
| 155 | if ($tblbloguser == null) { |
---|
| 156 | $this->setIsBlogMember(false); |
---|
| 157 | } else { |
---|
| 158 | $this->setIsBlogMember($tblbloguser->getUsRole()); |
---|
| 159 | } |
---|
| 160 | return $this->getAttribute('ismember'.blogStore::$blogId); |
---|
| 161 | } else return $isMember; |
---|
| 162 | } |
---|
| 163 | |
---|
| 164 | /** |
---|
| 165 | * Set current user is member of blog ($user_id==1) or not ($user_id==0) |
---|
| 166 | * |
---|
| 167 | * @param unknown_type $user_id |
---|
| 168 | */ |
---|
| 169 | public function setIsBlogMember($is_member) { |
---|
| 170 | $this->setAttribute('ismember'.blogStore::$blogId, $is_member); |
---|
| 171 | } |
---|
| 172 | |
---|
| 173 | /** |
---|
| 174 | * Clear blogmember cookie information |
---|
| 175 | * |
---|
| 176 | */ |
---|
| 177 | public function clearBlogmember() { |
---|
| 178 | if (!class_exists('blogStore')) return; |
---|
| 179 | $this->getAttributeHolder()->remove('ismember'.blogStore::$blogId); |
---|
| 180 | } |
---|
| 181 | |
---|
| 182 | /** |
---|
| 183 | * return status vote |
---|
| 184 | * false: never vote yet |
---|
| 185 | * true: voted |
---|
| 186 | * null: Nothing to vote |
---|
| 187 | */ |
---|
| 188 | public function isVote() { |
---|
| 189 | if (blogStore::$tblblogpoll==null) return null; |
---|
| 190 | $cookieName = 'isvote'.blogStore::$tblblogpoll->getPollId(); |
---|
| 191 | $isvote = $this->getAttribute($cookieName); |
---|
| 192 | if ($isvote === null) { |
---|
| 193 | $c = new Criteria(); |
---|
| 194 | $c->add(TblblogpolldataPeer::PD_POLL, blogStore::$tblblogpoll->getPollId()); |
---|
| 195 | $c->add(TblblogpolldataPeer::PD_IP, $_SERVER['REMOTE_ADDR']); |
---|
| 196 | $num = TblblogpolldataPeer::doCount($c); |
---|
| 197 | $this->setAttribute($cookieName, $num>0?1:0); |
---|
| 198 | return $num>0; |
---|
| 199 | } else return $isvote==1; |
---|
| 200 | } |
---|
| 201 | |
---|
| 202 | public function clearVote() { |
---|
| 203 | if (!class_exists('blogStore')) return; |
---|
| 204 | if (blogStore::$tblblogpoll==null) return; |
---|
| 205 | $cookieName = 'isvote'.blogStore::$tblblogpoll->getPollId(); |
---|
| 206 | $this->getAttributeHolder()->remove($cookieName); |
---|
| 207 | } |
---|
| 208 | } |
---|