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); } }
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); $groupNumbers = $groupDAO->getNumbers($groupId); foreach (array_keys($groupNumbers) as $number) { if ($number[0] != '+') { $number = '+' . $number; } $contacts[$number] = $number; } } $contacts = array_keys($contacts);
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]; }
/** * Get message edit form */ public static function getEditPage($id) { $app = \Slim\Slim::getInstance(); $dao = new MessageDAO(); $userDAO = new UserDAO(); $groupDAO = new GroupDAO(); $groupsQuery = array(); $senderDAO = new SenderDAO(); $sendersQuery = array(); $strong = \Strong\Strong::getInstance(); $user = $strong->getUser(); $groupsQuery['user_id'] = $user['id']; $sendersQuery['user_id'] = $user['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'); } $allGroups = $groupDAO->getList(); $allSenders = $senderDAO->getList(); if (!$userDAO->hasRole('ADMIN')) { // return $app->status(403); } $app->expires(time()); $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); } } $statuses = $dao->getStatuses($item); $tableTargets = array(); $hasErrors = false; $syncedNumbers = $groupDAO->getNumbers($item->getGroupId(), true); $targetTotal = array(); foreach (array_keys($syncedNumbers) as $target) { $targetTotal[$target] = $target; } $targetSent = array(); $targetReceived = array(); if (!empty($statuses)) { foreach ($statuses as $status) { if (!empty($status['target'])) { $target = $status['target']; if ($status['status'] == Message::MESSAGE_STATUS_RECEIVED_BY_SERVER) { $targetReceived[$status['target']] = $status['target']; } if ($status['status'] == Message::MESSAGE_STATUS_RECEIVED_BY_PHONE) { $targetReceived[$status['target']] = $status['target']; } if ($status['status'] == Message::MESSAGE_STATUS_SENT) { $targetSent[$status['target']] = $status['target']; } switch ($status['status']) { case Message::MESSAGE_STATUS_SENT: case Message::MESSAGE_STATUS_RECEIVED_BY_SERVER: case Message::MESSAGE_STATUS_RECEIVED_BY_PHONE: if (empty($tableTargets[$target]) || $tableTargets[$target] < $status['status']) { $tableTargets[$target] = $status['status']; } break; } } if ($status['status'] == Message::MESSAGE_STATUS_ERROR) { $hasErrors = true; } } } foreach ($targetTotal as $target) { if (empty($tableTargets[$target])) { $tableTargets[$target] = ''; } } if ($item->getStime() == 0) { $item->setStime(time()); } $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/*'; } $sender = $senderDAO->fetch($item->getSenderId()); if (!empty($sender) && '' == $sender->getPassword()) { $app->view->set('senderToConfirm', $sender); } $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); $app->view->set('menu', 'messages'); $app->view->set('id', $id); $app->view->set('item', $item); $app->view->set('allGroups', $allGroups); $app->view->set('allSenders', $allSenders); $app->view->set('groups', $groups); $app->view->set('senders', $senders); $app->view->set('sender', $sender); $app->view->set('tableTargets', $tableTargets); $app->view->set('statuses', $statuses); $app->view->set('hasErrors', $hasErrors); $app->view->set('noSent', count($targetSent)); $app->view->set('noReceived', count($targetReceived)); $app->view->set('noTotal', count($targetTotal)); $app->view->set('mime', $mime); $app->render('messages/edit.twig.html'); }
/** * Process confirmation SMS code * * @param number $id */ public static function postConfirmPage($id) { $app = \Slim\Slim::getInstance(); $senderDAO = new SenderDAO(); $sender = $senderDAO->fetch($id); if (empty($sender) || empty($id)) { return $app->notFound(); } $smscode = $_POST['smscode']; $smscode = preg_replace('![^0-9]*!', '', $smscode); $errors = array(); try { $wdao = WhatsappDAO::instance($sender); $wdao->confirmSmsCode($smscode); $app->flash('info', 'Your confirmation code has been successfully verified'); $app->redirect(MAINURL . '/senders/edit/' . $id); } catch (\Exception $ex) { $errors['debug'] = array($ex->getMessage() . "\n" . $wdao->getDebugBuf()); $app->view->set('menu', 'senders'); $app->view->set('id', $id); $app->view->set('item', $sender); $app->view->set('errors', $errors); $app->render('senders/confirm.twig.html'); return; } }