Skip to content

Commit

Permalink
Merge pull request #24 from zadarma/fix
Browse files Browse the repository at this point in the history
Fix httpBuildQuery and sendSms methods
  • Loading branch information
zadarma-github authored Jan 17, 2023
2 parents 147592b + c699b5d commit bfdb755
Show file tree
Hide file tree
Showing 2 changed files with 54 additions and 35 deletions.
54 changes: 35 additions & 19 deletions lib/Api.php
Original file line number Diff line number Diff line change
Expand Up @@ -277,9 +277,15 @@ public function getPbxRedirection($pbxNumber)
* @return Statistics
* @throws ApiException
*/
public function getStatistics($start = null, $end = null, $sip = null, $costOnly = null, $type = null, $skip = null,
$limit = null)
{
public function getStatistics(
$start = null,
$end = null,
$sip = null,
$costOnly = null,
$type = null,
$skip = null,
$limit = null
) {
$params = [
'start' => $start,
'end' => $end,
Expand All @@ -306,9 +312,14 @@ public function getStatistics($start = null, $end = null, $sip = null, $costOnly
* @return PbxStatistics
* @throws ApiException
*/
public function getPbxStatistics($start = null, $end = null, $newFormat = true, $callType = null,
$skip = null, $limit = null)
{
public function getPbxStatistics(
$start = null,
$end = null,
$newFormat = true,
$callType = null,
$skip = null,
$limit = null
) {
$params = [
'start' => $start,
'end' => $end,
Expand Down Expand Up @@ -463,20 +474,20 @@ public function setPbxRecording($sipId, $status, $email = null, $speechRecogniti
/**
* Sending the SMS messages.
*
* @param string number Phone number, where to send the SMS message (several numbers can be specified,
* separated by comma);
* @param string message Message (standard text limit applies; the text will be separated into several SMS messages,
* @param string|array $to Phone number(s), where to send the SMS message (array of numbers can be specified);
* @param string $message Message (standard text limit applies; the text will be separated into several SMS messages,
* if the limit is exceeded);
* @param string caller_id Phone number, from which the SMS messages is sent (can be sent only from list of user's
* @param string $callerId Phone number, from which the SMS messages is sent (can be sent only from list of user's
* confirmed phone numbers).
* @return Sms
* @throws ApiException
*/
public function sendSms($to, $message, $callerId = null)
{
$to = array_map([self::class, 'filterNumber'], is_array($to) ? $to : [$to]);
$params = [
'number' => self::filterNumber($to),
'message' => $message
'number' => implode(',', $to),
'message' => $message,
];
if ($callerId) {
$params['caller_id'] = $callerId;
Expand Down Expand Up @@ -564,9 +575,12 @@ public function setPbxVoicemailRedirection($pbxNumber, $destination, $always, $g
if (!filter_var($destination, FILTER_VALIDATE_EMAIL)) {
throw new \BadFunctionCallException('Wrong email parameter');
}
if (!in_array($greeting, [self::PBX_REDIRECTION_NO_GREETING, self::PBX_REDIRECTION_OWN_GREETING,
self::PBX_REDIRECTION_STANDART_GREETING])
) {
$allowedRedirections = [
self::PBX_REDIRECTION_NO_GREETING,
self::PBX_REDIRECTION_OWN_GREETING,
self::PBX_REDIRECTION_STANDART_GREETING
];
if (!in_array($greeting, $allowedRedirections)) {
throw new \BadFunctionCallException('Wrong voicemailGreeting parameter');
}
$params = [
Expand All @@ -577,9 +591,11 @@ public function setPbxVoicemailRedirection($pbxNumber, $destination, $always, $g
'voicemail_greeting' => $greeting,
];
if ($greeting == self::PBX_REDIRECTION_OWN_GREETING) {
if( !$greetingFile || !file_exists($greetingFile) ||
!in_array(pathinfo($greetingFile, PATHINFO_EXTENSION), ['wav', 'mp3'])
){
if (
!$greetingFile
|| !file_exists($greetingFile)
|| !in_array(pathinfo($greetingFile, PATHINFO_EXTENSION), ['wav', 'mp3'])
) {
throw new \BadFunctionCallException('Greeting file does not exist or has wrong extension.');
}
$params['greeting_file'] = curl_file_create($greetingFile);
Expand Down Expand Up @@ -806,4 +822,4 @@ protected static function arrayToResultObj($array, $resultClassName)
}
return $array;
}
}
}
35 changes: 19 additions & 16 deletions lib/Client.php
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
<?php

namespace Zadarma_API;

use Exception;

class Client
Expand All @@ -12,7 +13,7 @@ class Client
private $key;
private $secret;
private $httpCode;
private $limits = array();
private $limits = [];

/**
* @param $key
Expand All @@ -36,38 +37,38 @@ public function __construct($key, $secret, $isSandbox = false)
* @throws Exception
*
*/
public function call($method, $params = array(), $requestType = 'get', $format = 'json')
public function call($method, $params = [], $requestType = 'get', $format = 'json')
{
if (!is_array($params)) {
throw new ApiException('Query params must be an array.');
}

$type = strtoupper($requestType);
if (!in_array($type, array('GET', 'POST', 'PUT', 'DELETE'))) {
if (!in_array($type, ['GET', 'POST', 'PUT', 'DELETE'])) {
$type = 'GET';
}
$params['format'] = $format;

$options = array(
CURLOPT_URL => $this->url . $method,
CURLOPT_CUSTOMREQUEST => $type,
$options = [
CURLOPT_URL => $this->url . $method,
CURLOPT_CUSTOMREQUEST => $type,
CURLOPT_CONNECTTIMEOUT => 10,
CURLOPT_RETURNTRANSFER => true,
CURLOPT_SSL_VERIFYPEER => false,
CURLOPT_SSL_VERIFYHOST => false,
CURLOPT_HEADERFUNCTION => array($this, 'parseHeaders'),
CURLOPT_HTTPHEADER => $this->getAuthHeader($method, $params),
);
CURLOPT_HEADERFUNCTION => [$this, 'parseHeaders'],
CURLOPT_HTTPHEADER => $this->getAuthHeader($method, $params),
];

$ch = curl_init();

if ($type == 'GET') {
$options[CURLOPT_URL] = $this->url . $method . '?' . $this->httpBuildQuery($params);
} else {
$options[CURLOPT_POST] = true;
if(array_filter($params, 'is_object')){
if (array_filter($params, 'is_object')) {
$options[CURLOPT_POSTFIELDS] = $params;
}else{
} else {
$options[CURLOPT_POSTFIELDS] = $this->httpBuildQuery($params);
}
}
Expand Down Expand Up @@ -121,12 +122,14 @@ public function encodeSignature($signatureString)
*/
private function getAuthHeader($method, $params)
{
$params = array_filter($params, function($a){return !is_object($a);});
$params = array_filter($params, function ($a) {
return !is_object($a);
});
ksort($params);
$paramsString = $this->httpBuildQuery($params);
$signature = $this->encodeSignature($method . $paramsString . md5($paramsString));

return array('Authorization: ' . $this->key . ':' . $signature);
return ['Authorization: ' . $this->key . ':' . $signature];
}

/**
Expand All @@ -138,7 +141,7 @@ private function getAuthHeader($method, $params)
private function parseHeaders($curl, $line)
{
if (preg_match('/^X-RateLimit-([a-z]+):\s([0-9]+)/i', $line, $match)) {
$this->limits[$match[1]] = (int) $match[2];
$this->limits[$match[1]] = (int)$match[2];
}

return strlen($line);
Expand All @@ -151,8 +154,8 @@ private function parseHeaders($curl, $line)
*
* @return string
*/
private function httpBuildQuery($params = array())
private function httpBuildQuery($params = [])
{
return http_build_query($params, null, '&', PHP_QUERY_RFC1738);
return http_build_query($params, '', '&', PHP_QUERY_RFC1738);
}
}

0 comments on commit bfdb755

Please sign in to comment.