public function send($to, $message) { $retVal = Db::query(['INSERT INTO "pms" ("from","to","message") VALUES (:id,:to,:message)', [':id' => $_SESSION['id'], ':to' => $to, ':message' => $message]], Db::FETCH_ERRSTR); $wentWell = $retVal == Db::NO_ERRSTR; if ($wentWell && $this->user->wantsPush($to) && Config\PUSHED_ENABLED) { try { $pushed = Pushed::connectIp(Config\PUSHED_PORT, Config\PUSHED_IP6); $msg = json_encode(['messageFrom' => html_entity_decode(User::getUsername(), ENT_QUOTES, 'UTF-8'), 'messageFromId' => (string) $this->user->getId(), 'messageBody' => substr(html_entity_decode($message, ENT_QUOTES, 'UTF-8'), 0, 2000)]); //truncate to 2000 chars because of possibile service limitations $pushed->push($to, $msg); } catch (PushedException $e) { Db::dumpException($e); } } return $retVal; }
use NERDZ\Core\User; use NERDZ\Core\Config; use NERDZ\Core\Utils; $user = new User(); try { if (!$user->isLogged()) { die(Utils::jsonResponse(['ERROR' => 'Not logged'])); } if (!isset($_GET['action'])) { die(Utils::jsonResponse(['ERROR' => 'Action not set'])); } $thisUser = $user->getId(); if (!NERDZ\Core\Security::floodPushRegControl()) { die(Utils::jsonResponse(['ERROR' => 'NO SPAM'])); } $pushed = Pushed::connectIp(Config\PUSHED_PORT, Config\PUSHED_IP6); $resp = []; switch ($_GET['action']) { case 'subscribe': if (!isset($_POST['service']) || !isset($_POST['deviceId'])) { die(Utils::jsonResponse(['ERROR' => 'Field not set'])); } $user->setPush($thisUser, true); if (!$pushed->exists($thisUser)) { if ($pushed->addUser($thisUser)[0] !== Pushed::$ACCEPTED) { die(Utils::jsonResponse(['ERROR' => 'Request rejected'])); } } if ($pushed->subscribe($thisUser, $_POST['service'], $_POST['deviceId'])[0] !== Pushed::$ACCEPTED) { die(Utils::jsonResponse(['ERROR' => 'Request rejected'])); }