Skip to content
This repository has been archived by the owner on Jun 6, 2021. It is now read-only.

Updates for new Buzz lib and PSR-7 requests & responses #168

Open
wants to merge 7 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 6 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 4 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1 +1,4 @@
vendor
composer.lock
vendor

.idea/
15 changes: 3 additions & 12 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -1,21 +1,12 @@
language: php

before_install:
# If PHP >= 5.6, download & install PHPunit 5.7 to avoid builds failures
- if php -r "exit( (int)! version_compare( '$TRAVIS_PHP_VERSION', '5.6', '>=' ) );"; then wget -O phpunit https://phar.phpunit.de/phpunit-5.7.phar && chmod +x phpunit && mkdir ~/bin && mv -v phpunit ~/bin; fi

php:
- 5.3
- 5.4
- 5.5
- 5.6
- 7.0
- 7.1
- 7.2
env:
- SYMFONY_VERSION=origin/master

before_script:
- wget http://getcomposer.org/composer.phar
- php composer.phar install
- composer install

script: phpunit --configuration phpunit.travis.xml
script: ./vendor/bin/phpunit --configuration phpunit.travis.xml
2 changes: 0 additions & 2 deletions DependencyInjection/Compiler/AddHandlerPass.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,7 @@

use Symfony\Component\DependencyInjection\Compiler\CompilerPassInterface,
Symfony\Component\DependencyInjection\ContainerBuilder,
Symfony\Component\DependencyInjection\Definition,
Symfony\Component\DependencyInjection\Reference,
RMS\PushNotificationsBundle\Device\Types,
Symfony\Component\DependencyInjection\Exception\ParameterNotFoundException;

class AddHandlerPass implements CompilerPassInterface
Expand Down
1 change: 0 additions & 1 deletion Service/EventListener.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@

namespace RMS\PushNotificationsBundle\Service;


class EventListener {

/**
Expand Down
8 changes: 4 additions & 4 deletions Service/EventListenerInterface.php
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
<?php
namespace RMS\PushNotificationsBundle\Service;


interface EventListenerInterface {
namespace RMS\PushNotificationsBundle\Service;

interface EventListenerInterface
{
public function onKernelTerminate ();
}
}
32 changes: 22 additions & 10 deletions Service/OS/AndroidGCMNotification.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,15 @@

namespace RMS\PushNotificationsBundle\Service\OS;

use Psr\Log\LoggerInterface;
use RMS\PushNotificationsBundle\Exception\InvalidMessageTypeException,
RMS\PushNotificationsBundle\Message\AndroidMessage,
RMS\PushNotificationsBundle\Message\MessageInterface;
use Buzz\Browser,
Buzz\Client\AbstractCurl,
Buzz\Client\Curl,
Buzz\Client\MultiCurl;
use Nyholm\Psr7\Request;
use Psr\Log\LoggerInterface;
use RMS\PushNotificationsBundle\Exception\InvalidMessageTypeException,
RMS\PushNotificationsBundle\Message\AndroidMessage,
RMS\PushNotificationsBundle\Message\MessageInterface;

class AndroidGCMNotification implements OSNotificationServiceInterface
{
Expand Down Expand Up @@ -63,6 +64,13 @@ class AndroidGCMNotification implements OSNotificationServiceInterface
*/
protected $logger;

/**
* Buzz request client options as associative array.
*
* @var array
*/
protected $clientOptions;

/**
* Constructor
*
Expand All @@ -77,13 +85,15 @@ public function __construct($apiKey, $useMultiCurl, $timeout, $logger, AbstractC
{
$this->useDryRun = $dryRun;
$this->apiKey = $apiKey;
// We'll need to set this per-request if `$client` was provided in constructor.
$this->clientOptions = array(
'timeout' => $timeout,
'verify_peer' => false,
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good spot @jhkchan. I see here this option is actually also being used to toggle CURLOPT_SSL_VERIFYHOST too. This means the result is actually a further weakening of security checks vs. the old setVerifyPeer(false) call.

But since the new Buzz doesn't let you separate the two, and I am assuming there was a reason for the old option call, changing to the new option seems like the only safe fix for now.

);
if (!$client) {
$client = ($useMultiCurl ? new MultiCurl() : new Curl());
$client = ($useMultiCurl ? new MultiCurl($this->clientOptions) : new Curl($this->clientOptions));
}
$client->setTimeout($timeout);

$this->browser = new Browser($client);
$this->browser->getClient()->setVerifyPeer(false);
$this->logger = $logger;
}

Expand Down Expand Up @@ -122,14 +132,16 @@ public function send(MessageInterface $message)

if (count($message->getGCMIdentifiers()) == 1) {
$data['to'] = $gcmIdentifiers[0];
$this->responses[] = $this->browser->post($this->apiURL, $headers, json_encode($data));
$request = new Request('POST', $this->apiURL, $headers, json_encode($data));
$this->responses[] = $this->browser->getClient()->sendRequest($request, $this->clientOptions);
} else {
// Chunk number of registration IDs according to the maximum allowed by GCM
$chunks = array_chunk($message->getGCMIdentifiers(), $this->registrationIdMaxCount);

foreach ($chunks as $registrationIDs) {
$data['registration_ids'] = $registrationIDs;
$this->responses[] = $this->browser->post($this->apiURL, $headers, json_encode($data));
$request = new Request('POST', $this->apiURL, $headers, json_encode($data));
$this->responses[] = $this->browser->getClient()->sendRequest($request, $this->clientOptions);
}
}

Expand Down
22 changes: 11 additions & 11 deletions Service/OS/AndroidNotification.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

namespace RMS\PushNotificationsBundle\Service\OS;

use Buzz\Client\Curl;
use RMS\PushNotificationsBundle\Exception\InvalidMessageTypeException,
RMS\PushNotificationsBundle\Message\AndroidMessage,
RMS\PushNotificationsBundle\Message\MessageInterface;
Expand Down Expand Up @@ -32,11 +33,11 @@ class AndroidNotification implements OSNotificationServiceInterface
protected $source;

/**
* Timeout in seconds for the connecting client
* Buzz request client options as associative array.
*
* @var int
* @var array
*/
protected $timeout;
protected $clientOptions;

/**
* Authentication token
Expand All @@ -51,14 +52,17 @@ class AndroidNotification implements OSNotificationServiceInterface
* @param $username
* @param $password
* @param $source
* @param $timeout
* @param int $timeout Timeout in seconds
*/
public function __construct($username, $password, $source, $timeout)
{
$this->username = $username;
$this->password = $password;
$this->source = $source;
$this->timeout = $timeout;
$this->clientOptions = array(
'timeout' => $timeout,
'verify_peer' => false,
);
$this->authToken = "";
}

Expand All @@ -80,9 +84,7 @@ public function send(MessageInterface $message)
$headers[] = "Authorization: GoogleLogin auth=" . $this->authToken;
$data = $message->getMessageBody();

$buzz = new Browser();
$buzz->getClient()->setVerifyPeer(false);
$buzz->getClient()->setTimeout($this->timeout);
$buzz = new Browser(new Curl($this->clientOptions));
$response = $buzz->post("https://android.apis.google.com/c2dm/send", $headers, http_build_query($data));

return preg_match("/^id=/", $response->getContent()) > 0;
Expand All @@ -106,9 +108,7 @@ protected function getAuthToken()
"service" => "ac2dm"
);

$buzz = new Browser();
$buzz->getClient()->setVerifyPeer(false);
$buzz->getClient()->setTimeout($this->timeout);
$buzz = new Browser(new Curl($this->clientOptions));
$response = $buzz->post("https://www.google.com/accounts/ClientLogin", array(), http_build_query($data));
if ($response->getStatusCode() !== 200) {
return false;
Expand Down
24 changes: 12 additions & 12 deletions Service/OS/BlackberryNotification.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,14 @@

namespace RMS\PushNotificationsBundle\Service\OS;

use Psr\Log\LoggerInterface;
use Psr\Log\LoggerInterface,
Psr\Http\Message\ResponseInterface;
use RMS\PushNotificationsBundle\Exception\InvalidMessageTypeException,
RMS\PushNotificationsBundle\Message\BlackberryMessage,
RMS\PushNotificationsBundle\Message\MessageInterface;
use Buzz\Browser,
Buzz\Listener\BasicAuthListener,
Buzz\Client\Curl;
Buzz\Client\Curl,
Buzz\Middleware\BasicAuthMiddleware;

class BlackberryNotification implements OSNotificationServiceInterface
{
Expand Down Expand Up @@ -91,10 +92,8 @@ protected function doSend(BlackberryMessage $message)
{
$separator = "mPsbVQo0a68eIL3OAxnm";
$body = $this->constructMessageBody($message, $separator);
$browser = new Browser(new Curl());
$browser->getClient()->setTimeout($this->timeout);
$listener = new BasicAuthListener($this->appID, $this->password);
$browser->addListener($listener);
$browser = new Browser(new Curl(array('timeout' => $this->timeout)));
$browser->addMiddleware(new BasicAuthMiddleware($this->appID, $this->password));

$url = "https://pushapi.na.blackberry.com/mss/PD_pushRequest";
if ($this->evaluation) {
Expand All @@ -113,7 +112,7 @@ protected function doSend(BlackberryMessage $message)
/**
* Builds the actual body of the message
*
* @param \RMS\PushNotificationsBundle\Message\BlackberryMessage $message
* @param \RMS\PushNotificationsBundle\Message\BlackberryMessage $message
* @param $separator
* @return string
*/
Expand Down Expand Up @@ -143,16 +142,17 @@ protected function constructMessageBody(BlackberryMessage $message, $separator)
* Handles and parses the response
* Returns a value indicating success/fail
*
* @param \Buzz\Message\Response $response
* @param ResponseInterface $response
* @return bool
*/
protected function parseResponse(\Buzz\Message\Response $response)
protected function parseResponse(ResponseInterface $response)
{
if (null !== $response->getStatusCode() && $response->getStatusCode() != 200) {
if (null !== $response->getStatusCode() && $response->getStatusCode() !== 200) {
return false;
}
$response->getBody()->rewind();
$doc = new \DOMDocument();
$doc->loadXML($response->getContent());
$doc->loadXML($response->getBody()->getContents());
$elems = $doc->getElementsByTagName("response-result");
if (!$elems->length) {
$this->logger->error('Response is empty');
Expand Down
14 changes: 9 additions & 5 deletions Service/OS/MicrosoftNotification.php
Original file line number Diff line number Diff line change
Expand Up @@ -31,9 +31,11 @@ class MicrosoftNotification implements OSNotificationServiceInterface
*/
public function __construct($timeout, $logger)
{
$this->browser = new Browser(new Curl());
$this->browser->getClient()->setVerifyPeer(false);
$this->browser->getClient()->setTimeout($timeout);
$options = array(
'timeout' => $timeout,
'verify_peer' => false,
);
$this->browser = new Browser(new Curl($options));
$this->logger = $logger;
}

Expand Down Expand Up @@ -61,10 +63,12 @@ public function send(MessageInterface $message)

$response = $this->browser->post($message->getDeviceIdentifier(), $headers, $xml->asXML());

if (!$response->isSuccessful()) {
if ($response->getStatusCode() < 200 || $response->getStatusCode() >= 300) {
$this->logger->error($response->getStatusCode(). ' : '. $response->getReasonPhrase());

return false;
}

return $response->isSuccessful();
return true;
}
}
1 change: 0 additions & 1 deletion Service/iOSFeedback.php
Original file line number Diff line number Diff line change
Expand Up @@ -109,5 +109,4 @@ protected function getStreamContext()

return $ctx;
}

}
Loading