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
use GitGis\Whatsapp\Model\WhatsappDAO;
function hex($str)
{
    $retVal = '';
    for ($i = 0; $i < strlen($str); $i++) {
        $retVal .= sprintf("%02X ", ord($str[$i]));
    }
    return $retVal;
}
function logToCronFile($msg)
{
    echo date("Y-m-d H:i:s") . ' ' . time() . " " . $msg . "\n";
    file_put_contents(__DIR__ . '/logs/whatsapp.' . date('Ymd') . '.log', date("Y-m-d H:i:s") . " " . $msg . "\n", FILE_APPEND | LOCK_EX);
}
ob_start();
$senderDAO = new SenderDAO();
$groupDAO = new GroupDAO();
$messageDAO = new MessageDAO();
$senders = $senderDAO->getList();
foreach ($senders['list'] as $sender) {
    if ('' == $sender->getPassword()) {
        logToCronFile("No password for sender: " . $sender->getUsername());
        continue;
    }
    $sender = $senderDAO->fetch($sender->getId());
    if (($sender->getFlags() & \GitGis\Whatsapp\Model\Sender::FLAG_UNSYNC) > 0) {
        logToCronFile("Syncing " . $sender->getUsername());
        $groups = $groupDAO->getList(array('user_id' => $sender->getUserId()));
        $contacts = array();
        foreach ($groups['list'] as $groupId => $group) {
            $groupDAO->clearSynced($groupId);
Exemplo n.º 3
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.º 4
0
 /**
  * Process message form
  */
 public static function postEditPage($id)
 {
     $app = \Slim\Slim::getInstance();
     $dao = new MessageDAO();
     $userDAO = new UserDAO();
     $strong = \Strong\Strong::getInstance();
     $user = $strong->getUser();
     $user = $userDAO->fetch($user['id']);
     $groupDAO = new GroupDAO();
     $groupsQuery = array();
     $senderDAO = new SenderDAO();
     $sendersQuery = array();
     $strong = \Strong\Strong::getInstance();
     $user2 = $strong->getUser();
     $groupsQuery['user_id'] = $user2['id'];
     $sendersQuery['user_id'] = $user2['id'];
     $groups = $groupDAO->getList($groupsQuery);
     if (0 == $groups['total']) {
         return $app->redirect(MAINURL . '/groups');
     }
     $senders = $senderDAO->getList($sendersQuery);
     if (0 == $senders['total']) {
         return $app->redirect(MAINURL . '/senders');
     }
     $app->view->set('KIND_TEXT_MSG', Message::KIND_TEXT_MSG);
     $app->view->set('KIND_PHOTO_MSG', Message::KIND_PHOTO_MSG);
     $app->view->set('KIND_AUDIO_MSG', Message::KIND_AUDIO_MSG);
     $app->view->set('KIND_VIDEO_MSG', Message::KIND_VIDEO_MSG);
     $item = $dao->fetch($id);
     if (empty($item)) {
         return $app->notFound();
     } else {
         if (!$userDAO->hasRole('ADMIN') && $item->getGroupId() > 0 && !in_array($item->getGroupId(), array_keys($groups['list']))) {
             return $app->status(403);
         }
         if (empty($_POST['data'])) {
             $_POST['data'] = $item->getData();
         }
     }
     $statuses = $dao->getStatuses($item);
     if (!empty($_POST['resend'])) {
         $dao->resend($item);
         $app->redirect(MAINURL . '/messages/edit/' . $item->getId());
         return;
     }
     $item->dataHuman = self::getHumanUrl($item);
     $mime = '*/*';
     if (Message::KIND_PHOTO_MSG == $item->getKind()) {
         $mime = 'image/*';
     }
     if (Message::KIND_AUDIO_MSG == $item->getKind()) {
         $mime = 'audio/*';
     }
     if (Message::KIND_VIDEO_MSG == $item->getKind()) {
         $mime = 'video/*';
     }
     $item->setUserId($user->getId());
     $dateParts = explode('-', $_POST['stime_date']);
     $timeParts = explode(':', $_POST['stime_time']);
     $stime = mktime($timeParts[0], $timeParts[1], $timeParts[2], $dateParts[1], $dateParts[2], $dateParts[0]);
     $item->setStime($stime);
     if ($item->getKind() == Message::KIND_TEXT_MSG) {
         $item->setData($_POST['data']);
     }
     $item->setGroupId($_POST['group_id']);
     $item->setSenderId($_POST['sender_id']);
     $personalized = array();
     $customField = $dao->getCustomFields($item);
     $numbers = $groupDAO->getNumbers($item->getGroupId());
     foreach ($customField as $num => $fields) {
         if (empty($numbers[$num])) {
             continue;
         }
         $personalized[$num] = $num;
     }
     $_POST['personalized'] = 'personalized $validator';
     $validator = new \Valitron\Validator($_POST);
     $validator->addRule('credits', function ($name, $value) use($user, $userDAO) {
         if ($userDAO->hasRole('ADMIN')) {
             return true;
         }
         return $user->getCredits() > 0;
     });
     $validator->addRule('time', function ($name, $value) {
         $value = explode(':', $value);
         if (count($value) != 3) {
             return false;
         }
         return true;
     });
     $validator->addRule('personalized', function ($name, $value) use($personalized, $numbers) {
         if (count($personalized) == 0) {
             return true;
         }
         return count($personalized) == count($numbers);
     });
     $validator->rule('personalized', 'personalized');
     $validator->label('Personalized');
     $validator->rule('required', 'group_id');
     $validator->label('Group');
     $validator->rule('required', 'sender_id');
     $validator->label('Sender');
     $validator->rule('date', 'stime_date');
     $validator->label('Date');
     $validator->rule('time', 'stime_time');
     $validator->label('Time');
     $validator->rule('required', 'data');
     $validator->label('Message');
     $validator->rule('credits', 'credits');
     $validator->label('Credits');
     if ($validator->validate()) {
         $item = $dao->save($item);
         if (!empty($_POST['send']) && empty($statuses)) {
             $dao->addStatus($item, Message::MESSAGE_STATUS_TO_SEND);
             if (!$userDAO->hasRole('ADMIN')) {
                 $numbers = $groupDAO->getNumbers($item->getGroupId());
                 $user->setCredits($user->getCredits() - count($numbers));
                 $userDAO->save($user);
             }
         }
         $app->redirect(MAINURL . '/messages/edit/' . $item->getId());
     } else {
         $app->view->set('menu', 'messages');
         $app->view->set('id', $id);
         $app->view->set('item', $item);
         $app->view->set('numbers', $numbers);
         $app->view->set('numbers_count', count($numbers));
         $app->view->set('personalized', $personalized);
         $app->view->set('personalized_count', count($personalized));
         $app->view->set('groups', $groups);
         $app->view->set('senders', $senders);
         $app->view->set('statuses', $statuses);
         $app->view->set('errors', $validator->errors());
         $app->view->set('mime', $mime);
         $app->render('messages/edit.twig.html');
     }
 }
Exemplo n.º 5
0
 /**
  * Deletes sender
  */
 public static function postDeletePage($id)
 {
     $app = \Slim\Slim::getInstance();
     $userDAO = new UserDAO();
     if (!$userDAO->hasRole('ADMIN')) {
         return $app->status(403);
     }
     if (!empty($_POST['yes'])) {
         $dao = new SenderDAO();
         $dao->delete($id);
     }
     return $app->redirect(MAINURL . '/senders');
 }