Exemplo n.º 1
0
 public static function forceSync()
 {
     $senderDAO = new SenderDAO();
     $strong = \Strong\Strong::getInstance();
     $user = $strong->getUser();
     $sendersQuery = array();
     $sendersQuery['user_id'] = $user['id'];
     $senders = $senderDAO->getList($sendersQuery);
     foreach ($senders['list'] as $sender) {
         $sender = $senderDAO->fetch($sender->getId());
         if (empty($sender)) {
             continue;
         }
         $flags = $sender->getFlags();
         $flags |= Sender::FLAG_UNSYNC;
         $sender->setFlags($flags);
         $senderDAO->save($sender);
     }
 }
Exemplo 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];
 }
Exemplo n.º 3
0
            $groupNumbers = $groupDAO->getNumbers($groupId);
            foreach (array_keys($groupNumbers) as $number) {
                if ($number[0] != '+') {
                    $number = '+' . $number;
                }
                $contacts[$number] = $number;
            }
        }
        $contacts = array_keys($contacts);
        try {
            $whatsDAO = WhatsappDAO::instance($sender);
            $whatsDAO->syncContacts($sender, $contacts);
            $flags = $sender->getFlags();
            $flags &= ~\GitGis\Whatsapp\Model\Sender::FLAG_UNSYNC;
            $sender->setFlags($flags);
            $senderDAO->save($sender);
        } catch (\Exception $ex) {
            echo $ex;
        }
    }
}
$messagesList = $messageDAO->getList(array('toSend' => time()));
$messages = $messagesList['list'];
usort($messages, function ($a, $b) {
    return $a->getGroupId() - $b->getGroupId();
});
logToCronFile("Fetched messages to send: " . count($messages));
foreach ($messages as $message) {
    $messageDAO->clearRetry($message);
    $groupId = $message->getGroupId();
    $sender = $senderDAO->fetch($message->getSenderId());
Exemplo n.º 4
0
 /**
  * Process edit sender form, validate, save to DB
  *
  * @param unknown $id
  * @return boolean
  */
 public static function postEditPage($id)
 {
     $app = \Slim\Slim::getInstance();
     $dao = new SenderDAO();
     $userDAO = new UserDAO();
     $item = $dao->fetch($id);
     if (empty($item)) {
         return $app->notFound();
     }
     $_POST['username'] = preg_replace('![^0-9]*!', '', $_POST['username']);
     $item->setNickname($_POST['nickname']);
     if (empty($id)) {
         $item->setUsername($_POST['username']);
     }
     if ($userDAO->hasRole('ADMIN')) {
         $item->setUserId($_POST['user_id']);
     } else {
         if (empty($id)) {
             $strong = \Strong\Strong::getInstance();
             $user = $strong->getUser();
             $item->setUserId($user['id']);
         }
     }
     $validator = new \Valitron\Validator($_POST);
     $validator->addRule('unique_username', function ($name, $value) use($id, $dao) {
         $list = $dao->getList(array('username' => $value));
         if (!empty($list['list'])) {
             foreach ($list['list'] as $item) {
                 if ($item->getId() != $id) {
                     return false;
                 }
             }
         }
         return true;
     }, 'is not unique');
     $validator->rule('unique_username', 'username');
     $validator->rule('required', 'nickname');
     $validator->rule('required', 'username');
     $validator->label('MSISDN');
     if ($validator->validate()) {
         $item = $dao->save($item);
         $app->flash('info', 'Sender ' . $item->getNickname() . ' has been created successfully');
         $app->redirect(MAINURL . '/senders/edit/' . $item->getId());
     } else {
         $app->view->set('menu', 'senders');
         $app->view->set('id', $id);
         $app->view->set('users', $userDAO->getList());
         $app->view->set('item', $item);
         $app->view->set('errors', $validator->errors());
         $app->render('senders/edit.twig.html');
     }
 }