Your IP : 3.147.27.117
<?php
namespace Bitrix\KdaImportexcel;
use Bitrix\Main\Localization\Loc;
Loc::loadMessages(__FILE__);
class Cloud
{
protected static $moduleId = 'esolutions.importexcel';
protected $services = array(
'yadisk' => '/^https?:\/\/yadi\.sk\//i',
'mailru' => '/^https?:\/\/cloud\.mail\.ru\/public\//i',
'gdrive' => array(
'/^https?:\/\/drive\.google\.com\/open\?id=/i',
'/^https?:\/\/docs\.google\.com\/spreadsheets\/d\/[^\/]+\//i'
),
'dropbox' => '/^https?:\/\/www\.dropbox\.com\/.*\?dl=0(\D|$)/i'
);
public function GetService($link)
{
foreach($this->services as $k=>$v)
{
if(is_array($v))
{
foreach($v as $v2)
{
if(preg_match($v2, $link)) return $k;
}
}
elseif(preg_match($v, $link)) return $k;
}
return false;
}
public function MakeFileArray($service, $path)
{
$method = ucfirst($service).'GetFile';
if(!is_callable(array($this, $method))) return false;
$tmpPath = static::GetTmpFilePath($path);
if(call_user_func_array(array($this, $method), array(&$tmpPath, $path)))
{
$arFile = \CFile::MakeFileArray($tmpPath);
if(strlen($arFile["type"])<=0)
$arFile["type"] = "unknown";
return $arFile;
}
else
{
return false;
}
}
public static function GetTmpFilePath($path)
{
$urlComponents = parse_url($path);
if ($urlComponents && strlen($urlComponents["path"]) > 0)
{
$urlComponents["path"] = urldecode($urlComponents['path']);
$tmpPath = \CFile::GetTempName('', bx_basename($urlComponents["path"]));
}
else
$tmpPath = \CFile::GetTempName('', bx_basename($path));
$dir = \Bitrix\Main\IO\Path::getDirectory($tmpPath);
\Bitrix\Main\IO\Directory::createDirectory($dir);
return $tmpPath;
}
public function YadiskGetFile(&$tmpPath, $path)
{
$token = \Bitrix\Main\Config\Option::get(static::$moduleId, 'YANDEX_APIKEY', '');
if(!$token) return false;
$arUrl = parse_url($path);
$subPath = '';
if(strpos($arUrl['path'], '/d/')===0 && preg_match('/^\/d\/[^\/]*\/./', $arUrl['path']))
{
$subPath = preg_replace('/^\/d\/[^\/]*\//', '/', $arUrl['path']);
if($subPath && strlen($subPath) < strlen($arUrl['path']))
{
$path = substr($path, 0, -strlen($subPath));
}
}
$client = new \Bitrix\Main\Web\HttpClient(array('socketTimeout'=>10));
$client->setHeader('Authorization', "OAuth ".$token);
$res = $client->get('https://cloud-api.yandex.net/v1/disk/public/resources/download?public_key='.urlencode($path).(strlen($subPath) > 0 ? '&path='.urlencode($subPath) : ''));
$arRes = \CUtil::JsObjectToPhp($res);
if(is_array($arRes) && $arRes['href'])
{
$arUrl = parse_url($arRes['href']);
$filename = preg_grep('/^filename=/', explode('&', $arUrl['query']));
if(count($filename)==1)
{
$filename = urldecode(substr(current($filename), 9));
if((!defined('BX_UTF') || !BX_UTF)) $filename = $GLOBALS['APPLICATION']->ConvertCharset($filename, 'UTF-8', 'CP1251');
$tmpPath = static::GetTmpFilePath($filename);
}
if($client->download($arRes['href'], $tmpPath))
{
$tmpPath = \Bitrix\Main\IO\Path::convertLogicalToPhysical($tmpPath);
return true;
}
}
return false;
}
public function MailruGetFile(&$tmpPath, $path)
{
$arUrl = parse_url($path);
if(isset($arUrl['fragment']) && strlen($arUrl['fragment']) > 0)
{
$path = substr($path, 0, -strlen($arUrl['fragment']) - 1);
}
$mr = \Bitrix\KdaImportexcel\Cloud\MailRu::GetInstance();
return $mr->download($tmpPath, $path);
}
public function GdriveGetFile(&$tmpPath, $path)
{
$client = new \Bitrix\Main\Web\HttpClient(array('socketTimeout'=>30));
if(preg_match('/^https?:\/\/docs\.google\.com\/spreadsheets\/d\/([^\/]+)\//i', $path, $m))
{
$tmpPath = static::GetTmpFilePath($m[1].'.xlsx');
$path = 'https://docs.google.com/spreadsheets/d/'.$m[1].'/export?format=xlsx';
}
elseif(preg_match('/id=([^&]+)/i', $path, $m))
{
$tmpPath = static::GetTmpFilePath($m[1].'.xlsx');
$path = 'https://docs.google.com/spreadsheets/d/'.$m[1].'/export?format=xlsx';
}
if($client->download($path, $tmpPath))
{
$hcd = $client->getHeaders()->get('content-disposition');
if($hcd && stripos($hcd, 'filename='))
{
$hcdParts = array_map('trim', explode(';', $hcd));
$hcdParts1 = preg_grep('/filename\*=UTF\-8\'\'/i', $hcdParts);
$hcdParts2 = preg_grep('/filename=/i', $hcdParts);
if(count($hcdParts1) > 0)
{
$hcdParts1 = explode("''", current($hcdParts1));
$fn = urldecode(trim(end($hcdParts1), '"\' '));
if((!defined('BX_UTF') || !BX_UTF)) $fn = $GLOBALS['APPLICATION']->ConvertCharset($fn, 'UTF-8', 'CP1251');
$fn = \Bitrix\Main\IO\Path::convertLogicalToPhysical($fn);
if(strpos($tmpPath, $fn)===false)
{
$tmpPath = \CKDAImportUtils::ReplaceFile($tmpPath, preg_replace('/\/[^\/]+$/', '/'.$fn, $tmpPath));
}
}
elseif(count($hcdParts2) > 0)
{
$hcdParts2 = explode('=', current($hcdParts2));
$fn = trim(end($hcdParts2), '"\' ');
if(strpos($tmpPath, $fn)===false)
{
$tmpPath = \CKDAImportUtils::ReplaceFile($tmpPath, preg_replace('/\/[^\/]+$/', '/'.$fn, $tmpPath));
}
}
}
return true;
}
return false;
}
public function DropboxGetFile(&$tmpPath, $path)
{
$client = new \Bitrix\Main\Web\HttpClient(array('socketTimeout'=>30));
$path = preg_replace('/(\?dl=0)(\D|$)/i', '?dl=1$2', $path);
if($client->download($path, $tmpPath))
{
$hcd = $client->getHeaders()->get('content-disposition');
if($hcd && stripos($hcd, 'filename='))
{
$hcdParts = array_map('trim', explode(';', $hcd));
$hcdParts1 = preg_grep('/filename\*=UTF\-8\'\'/i', $hcdParts);
$hcdParts2 = preg_grep('/filename=/i', $hcdParts);
if(count($hcdParts1) > 0)
{
$hcdParts1 = explode("''", current($hcdParts1));
$fn = urldecode(trim(end($hcdParts1), '"\' '));
if((!defined('BX_UTF') || !BX_UTF)) $fn = $GLOBALS['APPLICATION']->ConvertCharset($fn, 'UTF-8', 'CP1251');
$fn = \Bitrix\Main\IO\Path::convertLogicalToPhysical($fn);
if(strpos($tmpPath, $fn)===false)
{
$tmpPath = \CKDAImportUtils::ReplaceFile($tmpPath, preg_replace('/\/[^\/]+$/', '/'.$fn, $tmpPath));
}
}
elseif(count($hcdParts2) > 0)
{
$hcdParts2 = explode('=', current($hcdParts2));
$fn = trim(end($hcdParts2), '"\' ');
if(strpos($tmpPath, $fn)===false)
{
$tmpPath = \CKDAImportUtils::ReplaceFile($tmpPath, preg_replace('/\/[^\/]+$/', '/'.$fn, $tmpPath));
}
}
}
return true;
}
return false;
}
}