279 lines
9.1 KiB
PHP
279 lines
9.1 KiB
PHP
<?php
|
|
class SelectelApi {
|
|
|
|
public static function getTokenAndUrl() {
|
|
$token = null;
|
|
$storageUrl = null;
|
|
$ch = curl_init();
|
|
curl_setopt($ch, CURLOPT_URL, 'https://api.selcdn.ru/auth/v1.0');
|
|
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'GET');
|
|
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
|
|
curl_setopt($ch, CURLOPT_HTTPHEADER, ["X-Auth-User: 231642_dataflow", "X-Auth-Key: Iy62MEKG~Pv2mRFF"]);
|
|
curl_setopt($ch, CURLOPT_HEADER , true);
|
|
curl_setopt($ch, CURLOPT_NOBODY , true);
|
|
|
|
$result = curl_exec($ch);
|
|
//var_dump($result);
|
|
|
|
$http_code = curl_getinfo($ch, CURLINFO_HTTP_CODE);
|
|
|
|
if ($http_code == 204) {
|
|
$headers = SelectelApi::getHeaders($result);
|
|
if(isset($headers['X-Auth-Token'])){
|
|
$token = trim($headers['X-Auth-Token']);
|
|
} else if (isset($headers['x-auth-token'])){
|
|
$token = trim($headers['x-auth-token']);
|
|
}
|
|
if(isset($headers['X-Storage-Url'])){
|
|
$storageUrl = trim($headers['X-Storage-Url']);
|
|
} else if (isset($headers['x-storage-url'])){
|
|
$storageUrl = trim($headers['x-storage-url']);
|
|
}
|
|
}
|
|
|
|
curl_close($ch);
|
|
|
|
return [$token, $storageUrl];
|
|
}
|
|
|
|
// новый метод получения токена поле изменения АПИ Селектел
|
|
public static function getNewToken() {
|
|
$token = null;
|
|
$ch = curl_init();
|
|
curl_setopt($ch, CURLOPT_VERBOSE, true);
|
|
curl_setopt($ch, CURLOPT_STDERR, fopen($_SERVER['DOCUMENT_ROOT'] . '/curl_select.txt', 'a+'));
|
|
curl_setopt($ch, CURLOPT_URL, 'https://cloud.api.selcloud.ru/identity/v3/auth/tokens');
|
|
curl_setopt($ch, CURLOPT_POST, 1);
|
|
curl_setopt($ch, CURLOPT_POSTFIELDS, '{"auth":{"identity":{"methods":["password"],"password":{"user":{"name":"jwdataflow","domain":{"name":"231642"},"password":"Ff(C9306"}}},"scope":{"project":{"name":"JW_data","domain":{"name":"231642"}}}}}');
|
|
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
|
|
curl_setopt($ch, CURLOPT_HTTPHEADER, ["content-type: application/json"]);
|
|
curl_setopt($ch, CURLOPT_HEADER , true);
|
|
|
|
$result = curl_exec($ch);
|
|
//var_dump($result);
|
|
|
|
$http_code = curl_getinfo($ch, CURLINFO_HTTP_CODE);
|
|
|
|
if ($http_code == 201) {
|
|
$headers = SelectelApi::getHeaders($result);
|
|
if(isset($headers['X-Subject-Token'])){
|
|
$token = trim($headers['X-Subject-Token']);
|
|
} else if (isset($headers['x-subject-token'])){
|
|
$token = trim($headers['x-subject-token']);
|
|
}
|
|
}
|
|
|
|
curl_close($ch);
|
|
|
|
return $token;
|
|
}
|
|
|
|
public static function uploadFile($storageUrl, $token, $filePath, $storageFilePath, $contentType, $debag=false) {
|
|
$ch = curl_init();
|
|
|
|
curl_setopt($ch, CURLOPT_URL, $storageUrl .'/jwdata/' . $storageFilePath);
|
|
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
|
|
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'PUT');
|
|
curl_setopt($ch, CURLOPT_POSTFIELDS, file_get_contents($filePath));
|
|
curl_setopt($ch, CURLOPT_SAFE_UPLOAD, true);
|
|
curl_setopt($ch, CURLOPT_HTTPHEADER, ["X-Auth-Token: $token", "content-type: " . $contentType]);
|
|
|
|
curl_exec($ch);
|
|
|
|
$http_code = curl_getinfo($ch, CURLINFO_HTTP_CODE);
|
|
|
|
if($debag){
|
|
var_dump($http_code);
|
|
}
|
|
curl_close($ch);
|
|
|
|
if ($http_code == 201) {
|
|
return true;
|
|
} else {
|
|
return false;
|
|
}
|
|
}
|
|
|
|
public static function uploadUserFile($token, $filePath, $storageFilePath, $contentType, $debug=false) {
|
|
$ch = curl_init();
|
|
|
|
curl_setopt($ch, CURLOPT_VERBOSE, true);
|
|
curl_setopt($ch, CURLOPT_STDERR, fopen($_SERVER['DOCUMENT_ROOT'] . '/curl_select.txt', 'a+'));
|
|
curl_setopt($ch, CURLOPT_URL, 'https://swift.ru-1.storage.selcloud.ru/v1/4df7b82aa77b439194788ae24b24ff3b/jwuf/' . $storageFilePath);
|
|
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
|
|
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'PUT');
|
|
curl_setopt($ch, CURLOPT_POSTFIELDS, file_get_contents($filePath));
|
|
curl_setopt($ch, CURLOPT_SAFE_UPLOAD, true);
|
|
curl_setopt($ch, CURLOPT_HTTPHEADER, ["X-Auth-Token: $token", "content-type: " . $contentType]);
|
|
|
|
$result = curl_exec($ch);
|
|
|
|
$http_code = curl_getinfo($ch, CURLINFO_HTTP_CODE);
|
|
|
|
if($debug){
|
|
var_dump($http_code);
|
|
var_dump($result);
|
|
}
|
|
curl_close($ch);
|
|
|
|
if ($http_code == 201) {
|
|
return true;
|
|
} else {
|
|
return false;
|
|
}
|
|
}
|
|
|
|
public static function uploadPdfFile($storageUrl, $token, $filePath, $storageFilePath, $debag=false) {
|
|
$ch = curl_init();
|
|
|
|
curl_setopt($ch, CURLOPT_URL, $storageUrl .'/jwpdf/' . $storageFilePath);
|
|
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
|
|
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'PUT');
|
|
curl_setopt($ch, CURLOPT_POSTFIELDS, file_get_contents($filePath));
|
|
curl_setopt($ch, CURLOPT_SAFE_UPLOAD, true);
|
|
curl_setopt($ch, CURLOPT_HTTPHEADER, ["X-Auth-Token: $token", "content-type: application/pdf"]);
|
|
|
|
curl_exec($ch);
|
|
|
|
$http_code = curl_getinfo($ch, CURLINFO_HTTP_CODE);
|
|
|
|
if($debag){
|
|
var_dump($http_code);
|
|
}
|
|
curl_close($ch);
|
|
|
|
if ($http_code == 201) {
|
|
return true;
|
|
} else {
|
|
return false;
|
|
}
|
|
}
|
|
|
|
public static function uploadMp3File($storageUrl, $token, $filePath, $storageFilePath, $debag=false) {
|
|
$ch = curl_init();
|
|
|
|
curl_setopt($ch, CURLOPT_URL, $storageUrl .'/jwvoice/' . $storageFilePath);
|
|
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
|
|
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'PUT');
|
|
curl_setopt($ch, CURLOPT_POSTFIELDS, file_get_contents($filePath));
|
|
curl_setopt($ch, CURLOPT_SAFE_UPLOAD, true);
|
|
curl_setopt($ch, CURLOPT_HTTPHEADER, ["X-Auth-Token: $token", "content-type: audio/mpeg"]);
|
|
|
|
curl_exec($ch);
|
|
|
|
$http_code = curl_getinfo($ch, CURLINFO_HTTP_CODE);
|
|
|
|
if($debag){
|
|
var_dump($http_code);
|
|
}
|
|
curl_close($ch);
|
|
|
|
if ($http_code == 201) {
|
|
return true;
|
|
} else {
|
|
return false;
|
|
}
|
|
}
|
|
|
|
public static function deleteMp3File($storageUrl, $token, $storageFilePath) {
|
|
$ch = curl_init();
|
|
|
|
curl_setopt($ch, CURLOPT_URL, $storageUrl .'/jwvoice/' . $storageFilePath);
|
|
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
|
|
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'DELETE');
|
|
curl_setopt($ch, CURLOPT_HTTPHEADER, ["X-Auth-Token: $token"]);
|
|
|
|
curl_exec($ch);
|
|
|
|
$http_code = curl_getinfo($ch, CURLINFO_HTTP_CODE);
|
|
|
|
curl_close($ch);
|
|
|
|
if ($http_code == 204) {
|
|
return true;
|
|
} else {
|
|
return false;
|
|
}
|
|
}
|
|
|
|
public static function deletePdfFile($storageUrl, $token, $storageFilePath) {
|
|
$ch = curl_init();
|
|
|
|
curl_setopt($ch, CURLOPT_URL, $storageUrl .'/jwpdf/' . $storageFilePath);
|
|
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
|
|
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'DELETE');
|
|
curl_setopt($ch, CURLOPT_HTTPHEADER, ["X-Auth-Token: $token"]);
|
|
|
|
curl_exec($ch);
|
|
|
|
$http_code = curl_getinfo($ch, CURLINFO_HTTP_CODE);
|
|
|
|
curl_close($ch);
|
|
|
|
if ($http_code == 204) {
|
|
return true;
|
|
} else {
|
|
return false;
|
|
}
|
|
}
|
|
|
|
public static function deleteFile($storageUrl, $token, $storageFilePath) {
|
|
$ch = curl_init();
|
|
|
|
curl_setopt($ch, CURLOPT_URL, $storageUrl .'/jwdata/' . $storageFilePath);
|
|
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
|
|
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'DELETE');
|
|
curl_setopt($ch, CURLOPT_HTTPHEADER, ["X-Auth-Token: $token"]);
|
|
|
|
curl_exec($ch);
|
|
|
|
$http_code = curl_getinfo($ch, CURLINFO_HTTP_CODE);
|
|
|
|
curl_close($ch);
|
|
|
|
if ($http_code == 204) {
|
|
return true;
|
|
} else {
|
|
return false;
|
|
}
|
|
}
|
|
|
|
public static function deleteReqFile($storageUrl, $token, $storageFilePath) {
|
|
$ch = curl_init();
|
|
|
|
curl_setopt($ch, CURLOPT_URL, $storageUrl .'/jwuf/' . $storageFilePath);
|
|
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
|
|
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'DELETE');
|
|
curl_setopt($ch, CURLOPT_HTTPHEADER, ["X-Auth-Token: $token"]);
|
|
|
|
curl_exec($ch);
|
|
|
|
$http_code = curl_getinfo($ch, CURLINFO_HTTP_CODE);
|
|
|
|
var_dump($http_code);
|
|
|
|
curl_close($ch);
|
|
|
|
if ($http_code == 204) {
|
|
return true;
|
|
} else {
|
|
return false;
|
|
}
|
|
}
|
|
|
|
static function getHeaders($response){
|
|
|
|
if (!preg_match_all('/([A-Za-z\-]{1,})\:(.*)\\r/', $response, $matches)
|
|
|| !isset($matches[1], $matches[2])){
|
|
return false;
|
|
}
|
|
|
|
$headers = [];
|
|
|
|
foreach ($matches[1] as $index => $key){
|
|
$headers[$key] = $matches[2][$index];
|
|
}
|
|
|
|
return $headers;
|
|
}
|
|
} |