meraproject/module/integration/onlinepbx/model.php

111 lines
3.6 KiB
PHP
Raw Normal View History

<?
namespace integration;
class Onlinepbx {
const URL_MONGO_HISTORY = 'mongo_history/search.json';
static function onpbx_get_secret_key($domain, $apikey, $new=false){
$data = array('auth_key'=>$apikey);
if ($new){$data['new'] ='true';}
$ch = curl_init('https://api.onlinepbx.ru/'.$domain.'/auth.json');
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'POST');
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
curl_setopt($ch, CURLOPT_TIMEOUT, 60);
$res = json_decode(curl_exec($ch), true);
if ($res){return $res;}else{return false;}
}
static function onpbx_api_query($secret_key, $key_id, $url, $post=array(), $opt=array()){
$method = 'POST';
$date = @date('r');
if (is_array($post)){
foreach ($post as $key => $val){
if (is_string($key) && preg_match('/^@(.+)/', $val, $m)){
$post[$key] = array('name'=>basename($m[1]), 'data'=>base64_encode(file_get_contents($m[1])));
}
}
}
$post = http_build_query($post);
$content_type = 'application/x-www-form-urlencoded';
$content_md5 = hash('md5', $post);
$signature = base64_encode(hash_hmac('sha1', $method."\n".$content_md5."\n".$content_type."\n".$date."\n".$url."\n", $secret_key, false));
$headers = array('Date: '.$date, 'Accept: application/json', 'Content-Type: '.$content_type, 'x-pbx-authentication: '.$key_id.':'.$signature, 'Content-MD5: '.$content_md5);
$ch = curl_init('https://'.$url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'POST');
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, $post);
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
curl_setopt($ch, CURLOPT_TIMEOUT, 60);
$res = json_decode(curl_exec($ch), true);
if ($res){return $res;}else{return false;}
}
static function request(
$domain='',
$apikey='',
$secret_key='',
$key_id='',
$url = '',
$data = []
){
if(!$secret_key) {
$new = false;
$data_key_array = self::onpbx_get_secret_key($domain, $apikey, $new);
if (!$data_key_array) {
$new = true;
$data_key_array = self::onpbx_get_secret_key($domain, $apikey, $new);
}
$secret_key = $data_key_array['data']['key'];
$key_id = $data_key_array['data']['key_id'];
}
$url = 'api.onlinepbx.ru/'.$domain.'/'.$url;
//$post = array('uuid' => '');
/*
$post = [
'start_stamp_from'=>strtotime(date('Y-m-d')),
];
/**/
$data_array = self::onpbx_api_query($secret_key, $key_id, $url, $data);
if($data_array['status']==0){
$data_key_array = self::onpbx_get_secret_key($domain, $apikey, true);
$secret_key = $data_key_array['data']['key'];
$key_id = $data_key_array['data']['key_id'];
$data_array = self::onpbx_api_query($secret_key, $key_id, $url, $data);
}
$res = [
'secret_key'=>$secret_key,
'secret_key_id'=>$key_id,
'url'=>$url,
'data'=>$data,
'result'=>$data_array,
];
return $res;
}
}
/*
key jExsW1LvZmbufE0yj2f85XPIlAtGTSTjt1uWLTRiG4
SIP аккаунт для тестов
300
sovabest
*/