/** * Get list of messages */ public static function getPage($page = 0) { $app = \Slim\Slim::getInstance(); $app->expires(time()); $userDAO = new UserDAO(); $groupDAO = new GroupDAO(); $groupsQuery = array(); if (!$userDAO->hasRole('ADMIN')) { $strong = \Strong\Strong::getInstance(); $user = $strong->getUser(); $groupsQuery['user_id'] = $user['id']; } $groups = $groupDAO->getList($groupsQuery); if (0 == $groups['total']) { return $app->redirect(MAINURL . '/groups'); } $chatDAO = new ChatDAO(); $query = $_GET; $query['from'] = preg_replace('![^0-9]*!', '', $query['search']); if (!$userDAO->hasRole('ADMIN')) { $strong = \Strong\Strong::getInstance(); $user = $strong->getUser(); $query['user_id'] = $user['id']; } $pager = new Pager(MAINURL . '/inbox/', 25); $pager->setPage($page); $query = $pager->getQueryArray($query); $list = $chatDAO->getList($query); $pager->setCount(count($list['list'])); if (isset($list['total'])) { $pager->setTotal($list['total']); } $app->view->set('menu', 'inbox'); $app->view->set('query', $query); $app->view->set('result', $list); $app->view->set('pager', $pager); $app->render('inbox/list.twig.html'); }
public static function getInboxPage($page = 0) { $app = \Slim\Slim::getInstance(); $limit = 25; if ($_GET['format'] == 'csv') { $page = 0; $limit = 10000; } $userDAO = new UserDAO(); $chatDAO = new ChatDAO(); $formUtils = new FormUtils(); $startTime = $formUtils->toTimestamp($_GET['start_date'] . ' 00:00'); $endTime = $formUtils->toTimestamp($_GET['end_date'] . ' 23:59'); if ($endTime <= 0) { $endTime = time(); } if ($startTime <= 0) { $startTime = $endTime - 7 * 24 * 3600; } $app->view->set('start_date', $startTime); $app->view->set('end_date', $endTime); $query = $_GET; $query['start_date'] = $startTime; $query['end_date'] = $endTime; if (!$userDAO->hasRole('ADMIN')) { $strong = \Strong\Strong::getInstance(); $user = $strong->getUser(); $query['user_id'] = $user['id']; } $pager = new Pager(MAINURL . '/reports/inbox/?' . http_build_query($_GET), $limit); $pager->setPage($page); $query = $pager->getQueryArray($query); $list = $chatDAO->getList($query); $pager->setCount(count($list['list'])); if (isset($list['total'])) { $pager->setTotal($list['total']); } $reportTable = array(); foreach ($list['list'] as $message) { $reportTable[] = $message; } $app->view->set('menu', 'reports'); $app->view->set('reportTable', array_values($reportTable)); $app->view->set('query', $query); $app->view->set('pager', $pager); if ($_GET['format'] == 'csv') { header("Content-type: text/csv"); header("Content-disposition: attachment; filename=inbox.csv"); $app->render('reports/inbox.twig.csv'); } else { $app->render('reports/inbox.twig.html'); } }
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]; }