Esempio n. 1
0
 public function save(Chat $item)
 {
     if (empty($item)) {
         throw new \Exception('Empty item');
     }
     $db = DBConnection::getInstance();
     if ($item->getFromNickname() == '') {
         $sql = "SELECT nickname FROM numbers WHERE nickname<>'' AND target=:target ";
         $query = $db->prepare($sql);
         $query->bindParam('target', $item->getFrom());
         $query->execute();
         $row = $query->fetch();
         if (!empty($row['nickname'])) {
             $nickname = $row['nickname'];
             $item->setFromNickname($nickname);
         }
     }
     if ($item->getToNickname() == '') {
         $sql = "SELECT nickname FROM senders WHERE nickname<>'' AND username=:username ";
         $query = $db->prepare($sql);
         $query->bindParam('username', $item->getTo());
         $query->execute();
         $row = $query->fetch();
         if (!empty($row['nickname'])) {
             $nickname = $row['nickname'];
             $item->setToNickname($nickname);
         }
     }
     if ($item->getId() == 0) {
         $query = $db->prepare("SELECT * FROM chat WHERE whatsapp_id=:whatsapp_id ");
         $query->bindParam('whatsapp_id', $item->getWhatsappId());
         $query->execute();
         $row = $query->fetch();
         if (!empty($row)) {
             $item = new Chat();
             $item->setId($row['id']);
             $item->setData($row['data']);
             $item->setFrom($row['from']);
             $item->setFromNickname($row['from_nickname']);
             $item->setTo($row['to']);
             $item->setToNickname($row['to_nickname']);
             $item->setCtime($row['ctime']);
             $item->setWhatsappId($row['whatsapp_id']);
             return $item;
         }
         $query = $db->prepare("INSERT INTO chat\n\t\t\t\t\t(`from`, `to`, from_nickname, to_nickname, data, ctime, whatsapp_id)\n\t\t\t\t\tVALUES\n\t\t\t\t\t(:from, :to, :from_nickname, :to_nickname, :data, :ctime, :whatsapp_id)");
         $query->bindParam('from', $item->getFrom());
         $query->bindParam('to', $item->getTo());
         $query->bindParam('from_nickname', $item->getFromNickname());
         $query->bindParam('to_nickname', $item->getToNickname());
         $query->bindParam('data', $item->getData());
         $query->bindParam('ctime', $item->getCtime(), \PDO::PARAM_INT);
         $query->bindParam('whatsapp_id', $item->getWhatsappId());
         $query->execute();
         $item->setId($db->lastInsertId());
     } else {
         $sql = ' UPDATE chat SET ';
         $sql .= ' whatsapp_id = :whatsapp_id, ';
         $sql .= ' `from` = :from, ';
         $sql .= ' `to` = :to, ';
         $sql .= ' from_nickname = :from_nickname, ';
         $sql .= ' to_nickname= :to_nickname, ';
         $sql .= ' data = :data, ';
         $sql .= ' ctime = :ctime ';
         $sql .= ' WHERE id = :id ';
         $query = $db->prepare($sql);
         $query->bindParam('id', $item->getId(), \PDO::PARAM_INT);
         $query->bindParam('from', $item->getFrom());
         $query->bindParam('to', $item->getTo());
         $query->bindParam('from_nickname', $item->getFromNickname());
         $query->bindParam('to_nickname', $item->getToNickname());
         $query->bindParam('data', $item->getData());
         $query->bindParam('ctime', $item->getCtime());
         $query->bindParam('whatsapp_id', $item->getWhatsappId());
         $query->execute();
     }
     return $item;
 }
Esempio n. 2
0
 public function getWhatsProt(Sender $sender, $connectionRequired = true)
 {
     $username = $sender->getUsername();
     // Telephone number including the country code without '+' or '00'.
     $identity = $sender->getIdentity();
     // Obtained during registration with this API or using MissVenom (https://github.com/shirioko/MissVenom) to sniff from your phone.
     $nickname = $sender->getNickname();
     // This is the username displayed by WhatsApp clients.
     if (!empty($this->whatsProts[$username])) {
         $w = $this->whatsProts[$username];
         if ($connectionRequired && empty($this->connected)) {
             $w->cleanDebug();
             $w->connect();
             $w->loginWithPassword($sender->getPassword());
             $w->sendPassive("false");
             $this->connected = true;
             $w->sendGetServerProperties();
             echo $w->getDebugBuf();
         }
         return $w;
     }
     $groupDAO = new GroupDAO();
     $w = new WhatsProt($username, $identity, $nickname, true);
     $w->cleanDebug();
     $w->eventManager()->bind('onGetSyncResult', function (\SyncResult $syncResult) use($groupDAO, $sender) {
         $groups = $groupDAO->getList(array('user_id' => $sender->getUserId()));
         foreach (array_keys($syncResult->existing) as $number) {
             if ($number[0] == '+') {
                 $number = substr($number, 1);
             }
             foreach ($groups['list'] as $groupId => $group) {
                 $groupDAO->markSynced($groupId, $number);
             }
         }
         foreach ($syncResult->nonExisting as $number) {
             if ($number[0] == '+') {
                 $number = substr($number, 1);
             }
             // TODO
         }
     });
     $w->eventManager()->bind('onCredentialsBad', function ($number, $status, $reason) use($sender) {
         $senderDao = new SenderDAO();
         $senderMod = $senderDao->fetch($sender->getId());
         $senderMod->setPassword('');
         $senderDao->save($senderMod);
         throw new \Exception('Bad credentials for: ' . $number . ', status: ' . $status . ', reason: ' . $reason, 403);
     });
     $w->eventManager()->bind('onLoginFailed', function ($number) use($sender) {
         $senderDao = new SenderDAO();
         if ('' != $sender->getPassword()) {
             $wdao = WhatsappDAO::instance($sender);
             $wdao->sendSmsCode();
         } else {
             //            $senderMod = $senderDao->fetch($sender->getId());
             ////            $senderMod->setPassword('');
             //            $senderDao->save($senderMod);
         }
         throw new \Exception('Login failed for ' . $number, 403);
     });
     $w->eventManager()->bind('onSendMessage', function ($from, $target, $whatsapp_id, $node) {
         $this->lastSentMessageId = $whatsapp_id;
         if (empty($this->currentMessage)) {
             return;
         }
         $dao = new MessageDAO();
         if (empty($target) || is_array($target)) {
             if (!empty($this->currentTargets)) {
                 foreach ($this->currentTargets as $target) {
                     $dao->addStatus($this->currentMessage, Message::MESSAGE_STATUS_SENT, '', $target, $whatsapp_id);
                 }
             }
         } else {
             $target = explode('@', $target);
             $target = $target[0];
             $dao->addStatus($this->currentMessage, Message::MESSAGE_STATUS_SENT, '', $target, $whatsapp_id);
         }
     });
     $w->eventManager()->bind('onGetReceipt', function ($from, $id, $offline, $retry) {
         $target = explode('@', $from);
         $target = $target[0];
         $dao = new MessageDAO();
         $item = $dao->getMessageByWhatsappId($whatsapp_id);
         if (!empty($item)) {
             if (strpos($from, 'broadcast')) {
                 $target = explode('@', $node->getAttribute('participant'));
                 $target = $target[0];
                 if (!empty($target)) {
                     $dao->setMessageTargetStatus($item, $target, Message::MESSAGE_STATUS_RECEIVED_BY_PHONE);
                     $dao->addStatus($item, Message::MESSAGE_STATUS_RECEIVED_BY_PHONE, '', $target, $whatsapp_id);
                 }
             } else {
                 $dao->setMessageTargetStatus($item, $target, Message::MESSAGE_STATUS_RECEIVED_BY_PHONE);
                 $dao->addStatus($item, Message::MESSAGE_STATUS_RECEIVED_BY_PHONE, '', $target, $whatsapp_id);
             }
         }
     });
     $w->eventManager()->bind('onGetAck', function ($from, $whatsapp_id, $class) {
         if ($class != 'message') {
             return;
         }
         $target = explode('@', $from);
         $target = $target[0];
         $dao = new MessageDAO();
         $item = $dao->getMessageByWhatsappId($whatsapp_id);
         if (!empty($item)) {
             if (strpos($from, 'broadcast')) {
                 $dao->getSentTargetsById($whatsapp_id);
             } else {
                 $dao->setMessageTargetStatus($item, $target, Message::MESSAGE_STATUS_RECEIVED_BY_SERVER);
                 $dao->addStatus($item, Message::MESSAGE_STATUS_RECEIVED_BY_SERVER, '', $target, $whatsapp_id);
             }
         }
     });
     $w->eventManager()->bind('onGetMessage', function ($number, $from, $id, $type, $t, $name, $data) {
         if ($type == 'text') {
             $chatDAO = new ChatDAO();
             $from = preg_replace('!@.*!', '', $from);
             $chat = new Chat();
             $chat->setFrom($from);
             $chat->setFromNickname($name);
             $chat->setTo($number);
             $chat->setData($data);
             $chat->setCtime($t);
             $chat->setWhatsappId($id);
             $chatDAO->save($chat);
         }
     });
     if ($connectionRequired && empty($this->connected)) {
         $w->cleanDebug();
         $w->connect();
         $w->loginWithPassword($sender->getPassword());
         $w->sendGetStatuses("True");
         $this->connected = true;
         $w->sendGetServerProperties();
         echo $w->getDebugBuf();
     }
     $this->whatsProts[$username] = $w;
     return $this->whatsProts[$username];
 }