Skip to content
Open
Show file tree
Hide file tree
Changes from all 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
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
],
"require": {
"php": ">=5.5.0",
"guzzlehttp/guzzle": "^6.2"
"guzzlehttp/guzzle": ">=6.2"
},
"autoload": {
"psr-4": {
Expand Down
2 changes: 1 addition & 1 deletion src/Api/Response.php
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ public static function create(\GuzzleHttp\Psr7\Response $response)
if (isset($data['status'])) {
if ($data['status'] != 0) {
throw new ApiException('Remote error: ' .
(isset($data['status_message']) ? $data['status_message'] : '-'),
(isset($data['status_message']) ? $data['status_message'] : $response->getBody()),
$data['status']);
}
$item = new self();
Expand Down
36 changes: 30 additions & 6 deletions src/Bot.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,20 @@ class Bot
*/
protected $managers = [];

/**
* Signature string
*
* @var string
*/
protected $signature;

/**
* Input body
*
* @var string
*/
protected $body;

/**
* Init client
*
Expand All @@ -49,6 +63,14 @@ public function __construct(array $options)
} else {
throw new \RuntimeException('Specify "client" or "token" parameter');
}

if (isset($options['signature'])) {
$this->signature = $options['signature'];
}

if (isset($options['body'])) {
$this->body = $options['body'];
}
}

/**
Expand Down Expand Up @@ -150,16 +172,18 @@ public function onPicture(\Closure $handler)
* @throws \RuntimeException
* @return string
*/
public function getSignHeaderValue()
public function getSignValue()
{
$signature = '';
if (isset($_SERVER['HTTP_X_VIBER_CONTENT_SIGNATURE'])) {
if ($this->signature !== null) {
$signature = $this->signature;
} elseif (isset($_SERVER['HTTP_X_VIBER_CONTENT_SIGNATURE'])) {
$signature = $_SERVER['HTTP_X_VIBER_CONTENT_SIGNATURE'];
} elseif (isset($_GET['sig'])) {
$signature = $_GET['sig'];
}
if (empty($signature)) {
throw new \RuntimeException('Signature header not found', 1);
throw new \RuntimeException('Signature not found', 1);
}

return $signature;
Expand All @@ -172,7 +196,7 @@ public function getSignHeaderValue()
*/
public function getInputBody()
{
return file_get_contents('php://input');
return $this->body ?? fopen('php://input', 'r');
}

/**
Expand Down Expand Up @@ -201,11 +225,11 @@ public function run($event = null)
$eventBody = $this->getInputBody();

if (!Signature::isValid(
$this->getSignHeaderValue(),
$this->getSignValue(),
$eventBody,
$this->getClient()->getToken()
)) {
throw new \RuntimeException('Invalid signature header', 2);
throw new \RuntimeException('Invalid signature', 2);
}
// check json
$eventBody = json_decode($eventBody, true);
Expand Down