private function searchUser(User $from, $userId)
 {
     if ($userId == '') {
         return null;
     }
     if ($userId == $from->getId()) {
         return $from;
     }
     $form = (new Form())->import([UserDAO::ID => $userId])->addRule(UserDAO::ID, Rules::isUserOnline(), $from->getLang()->getPhrase('UserIsNotOnline'));
     if (!$form->validate()) {
         RespondError::make($from, $form->getErrors());
         DI::get()->getLogger()->warn("Trying to find userId = {$userId} for private message but not found", [__CLASS__]);
         return false;
     }
     $recipient = $form->getResult(UserDAO::ID);
     /* @var $recipient User */
     return $recipient;
 }
 /**
  * @param $sessionInfo
  * @param UserCollection $clients
  * @param Logger $logger
  * @param User $newUserWrapper
  * @return UserDAO
  */
 private function handleKnownUser($sessionInfo, UserCollection $clients, Logger $logger, User $newUserWrapper)
 {
     $user = UserDAO::create()->getById($sessionInfo['user_id']);
     $lang = $newUserWrapper->getLang();
     if ($oldClient = $clients->getClientById($user->getId())) {
         if ($timer = $oldClient->getDisconnectTimer()) {
             DI::get()->container()->get('eventloop')->cancelTimer($timer);
             $logger->info("Deffered disconnection timer canceled: connection_id = {$newUserWrapper->getConnectionId()} for user_id = {$sessionInfo['user_id']}", [__METHOD__]);
             if ($oldClient->getConnectionId()) {
                 $oldClient->setAsyncDetach(false)->send(['disconnect' => 1]);
                 $clients->detach($oldClient);
                 $newUserWrapper->setLastMsgId(-1);
             }
         } elseif ($oldClient->getConnectionId()) {
             // If there is no timer set, then
             // 1) it's regular user visit
             // 2) an attempt to open another browser tab
             // 3) window reload
             $oldClient->setAsyncDetach(false)->send(['msg' => $lang->getPhrase('DuplicateConnection'), 'disconnect' => 1]);
             $clients->detach($oldClient);
             if ($oldClient->getIp() == $newUserWrapper->getIp()) {
                 $newUserWrapper->setLastMsgId(-1);
             }
             $logger->info("Probably tabs duplication detected: detaching = {$oldClient->getConnectionId()} for user_id = {$oldClient->getId()}}", [__METHOD__]);
         }
         if ($newUserWrapper->getLastMsgId()) {
             $logger->info("Re-established connection for user_id = {$sessionInfo['user_id']}, lastMsgId = {$newUserWrapper->getLastMsgId()}", [__METHOD__]);
         }
     }
     return $user;
 }
Beispiel #3
0
 /**
  * @param User $from
  */
 private function respondOnMalformedJSON(User $from)
 {
     $response = (new ErrorResponse())->setErrors(['request' => $from->getLang()->getPhrase('MalformedJsonRequest')])->setChannelId($from->getChannelId());
     (new UserCollection())->attach($from)->setResponse($response)->notify();
 }
 /**
  * @param User $user
  */
 private function respondFloodError(User $user)
 {
     $response = (new ErrorResponse())->setChannelId($user->getChannelId())->setErrors(['flood' => $user->getLang()->getPhrase('FloodDetected')]);
     (new UserCollection())->setResponse($response)->attach($user)->notify(false);
 }
 private function handleNameChange(User $user, $request)
 {
     $name = $request[PropertiesDAO::NAME];
     $guestName = $user->getLang()->getPhrase('Guest');
     $nameChangeFreq = DI::get()->getConfig()->nameChangeFreq;
     $isNewbie = mb_strpos($name, $guestName) !== false;
     $changeLog = NameChangeDAO::create()->getLastByUser($user);
     $hasNameChanged = $name != $user->getProperties()->getName();
     if ($isNewbie) {
         $newname = str_replace($guestName, TimEnum::create($request[PropertiesDAO::TIM])->getShortName(), $name);
         $duplUser = PropertiesDAO::create()->getByUserName($newname);
         if (!($duplUser->getId() && $duplUser->getUserId() != $user->getId())) {
             $name = $newname;
         }
     } elseif ($changeLog && $hasNameChanged && !$this->isExpired($changeLog, $nameChangeFreq)) {
         RespondError::make($user, $user->getLang()->getPhrase('NameChangePolicy', date('Y-m-d H:i', $changeLog->getDate() + $nameChangeFreq)));
         return;
     }
     if ($changeLog = NameChangeDAO::create()->getLastByName($name)) {
         if ($changeLog->getUserId() != $user->getId()) {
             if (!$this->isExpired($changeLog, $nameChangeFreq)) {
                 RespondError::make($user, $user->getLang()->getPhrase('NameTakePolicy', date('Y-m-d H:i', $changeLog->getDate() + $nameChangeFreq)));
                 return;
             }
         }
     }
     if ($hasNameChanged && !$isNewbie) {
         NameChangeDAO::create()->setUser($user)->save();
     }
 }
 public static function make(User $user, $errors = null)
 {
     $response = (new ErrorResponse())->setErrors(is_array($errors) ? $errors : [$errors ?: $user->getLang()->getPhrase('RequiredActionNotSpecified')])->setChannelId($user->getChannelId());
     (new UserCollection())->attach($user)->setResponse($response)->notify();
 }