/** * @param QuarkClient $client * * @return mixed */ public function Client(QuarkClient $client) { $conn = $client->Connect(); if (!$conn) { Quark::Log('Mail. Unable to connect to mail server. Error: ' . $client->Error(true)); return false; } $smtp = $this->_config->SMTP(); $this->_dto->Header(QuarkDTO::HEADER_CONTENT_TRANSFER_ENCODING, QuarkDTO::TRANSFER_ENCODING_BASE64); try { $this->_cmd($client, 220); $this->_cmd($client, 250, 'HELO Quark'); $this->_cmd($client, 334, 'AUTH LOGIN'); $this->_cmd($client, 334, base64_encode($smtp->user)); $this->_cmd($client, 235, base64_encode($smtp->pass)); $this->_cmd($client, 250, 'MAIL FROM: <' . $smtp->user . '>'); foreach ($this->_receivers as $receiver) { $this->_cmd($client, 250, 'RCPT TO: <' . $receiver . '>'); } $this->_cmd($client, 354, 'DATA'); $this->_cmd($client, 250, $this->_dto->SerializeResponseBody() . "\r\n."); $this->_cmd($client, 221, 'QUIT'); } catch (QuarkArchException $e) { return false; } return $client->Close(); }
/** * @param QuarkClient $client * @param string $data * * @return mixed * @throws \Exception */ public function OnData(QuarkClient $client, $data) { if ($client->Connected()) { try { $this->_buffer .= $data; $out = $this->_processor->Decode($this->_buffer); if ($out !== false) { $this->_buffer = ''; if ($this->_protocol) { $this->_protocol->OnData($client, $out); } } } catch (\Exception $e) { $this->_buffer = ''; } } else { if ($data != "\r\n") { $this->_buffer .= $data; return; } $request = new QuarkDTO(); $request->UnserializeRequest($this->_buffer . "\r\n"); $this->_buffer = ''; $response = new QuarkDTO(new QuarkHTMLIOProcessor()); $response->Status(101, 'Switching Protocols'); $response->Headers(array(QuarkDTO::HEADER_CONNECTION => QuarkDTO::CONNECTION_UPGRADE, QuarkDTO::HEADER_UPGRADE => QuarkDTO::UPGRADE_WEBSOCKET, QuarkDTO::HEADER_SEC_WEBSOCKET_ACCEPT => base64_encode(sha1($request->Header(QuarkDTO::HEADER_SEC_WEBSOCKET_KEY) . self::GuID, true)))); if (strlen($this->_subprotocol) != 0) { $response->Header(QuarkDTO::HEADER_SEC_WEBSOCKET_PROTOCOL, $this->_subprotocol); } $client->Send($response->SerializeResponse()); $client->Connected(true); $client->BeforeSend(function ($data) { return $this->_processor->Encode($data); }); if ($this->_protocol) { $this->_protocol->OnConnect($client); } } }
/** * @param $payload * @param $options * * @return mixed */ public function Send($payload, $options = []) { $type = isset($options[self::OPTION_TYPE]) ? $options[self::OPTION_TYPE] : self::TYPE_TOAST; $badge = isset($options[self::OPTION_VALUE]) ? $options[self::OPTION_VALUE] : null; $visual = ''; $templates = isset($options[self::OPTION_VISUAL]) && is_array($options[self::OPTION_VISUAL]) ? $options[self::OPTION_VISUAL] : array(); foreach ($templates as $elem) { if ($elem instanceof MicrosoftNotificationTemplate) { $visual .= $elem->Binding(); } } $request = new QuarkDTO(new QuarkPlainIOProcessor()); $request->Method('POST'); $request->Header(QuarkDTO::HEADER_AUTHORIZATION, 'Bearer ' . $this->_token()); $request->Header('X-WNS-Type', $type); $request->Header(QuarkDTO::HEADER_CONTENT_TYPE, 'text/xml'); $type = str_replace('wns/', '', $type); $data = '<?xml version="1.0" encoding="utf-8"?> <' . $type . ($badge === null ? '' : ' value="' . $badge . '"') . '>' . ($type == self::TYPE_BADGE ? '' : '<visual>' . $visual . '</visual>') . '<data>' . json_encode($payload) . '</data> </' . $type . '>'; $request->Data($data); $response = new QuarkDTO(new QuarkPlainIOProcessor()); foreach ($this->_devices as $device) { QuarkHTTPTransportClient::To($device->id, $request, $response); } }