Пример #1
0
 public function prepareUser($arFBUser, $short = false)
 {
     $arFields = array('EXTERNAL_AUTH_ID' => self::ID, 'XML_ID' => $arFBUser["id"], 'LOGIN' => "FB_" . $arFBUser["id"], 'EMAIL' => $arFBUser["email"] != '' ? $arFBUser["email"] : '', 'NAME' => $arFBUser["first_name"], 'LAST_NAME' => $arFBUser["last_name"], 'OATOKEN' => $this->entityOAuth->getToken(), 'OATOKEN_EXPIRES' => $this->entityOAuth->getAccessTokenExpires());
     if (!$short && isset($arFBUser['picture']['data']['url']) && !$arFBUser['picture']['data']['is_silhouette']) {
         $picture_url = CFacebookInterface::GRAPH_URL . '/' . $arFBUser['id'] . '/picture?type=large';
         $temp_path = CFile::GetTempName('', 'picture.jpg');
         $ob = new HttpClient(array("redirect" => true));
         $ob->download($picture_url, $temp_path);
         $arPic = CFile::MakeFileArray($temp_path);
         if ($arPic) {
             $arFields["PERSONAL_PHOTO"] = $arPic;
         }
     }
     if (isset($arFBUser['birthday'])) {
         if ($date = MakeTimeStamp($arFBUser['birthday'], "MM/DD/YYYY")) {
             $arFields["PERSONAL_BIRTHDAY"] = ConvertTimeStamp($date);
         }
     }
     if (isset($arFBUser['gender']) && $arFBUser['gender'] != '') {
         if ($arFBUser['gender'] == 'male') {
             $arFields["PERSONAL_GENDER"] = 'M';
         } elseif ($arFBUser['gender'] == 'female') {
             $arFields["PERSONAL_GENDER"] = 'F';
         }
     }
     $arFields["PERSONAL_WWW"] = $this->getProfileUrl($arFBUser['id']);
     if (strlen(SITE_ID) > 0) {
         $arFields["SITE_ID"] = SITE_ID;
     }
     return $arFields;
 }
Пример #2
0
 /**
  * Download part of file from cloud service by FileData::id, put contents in FileData::src
  * @param FileData $fileData
  * @param          $startRange
  * @param          $chunkSize
  * @return FileData|null
  */
 public function downloadPartFile(FileData $fileData, $startRange, $chunkSize)
 {
     if (!$this->checkRequiredInputParams($fileData->toArray(), array('id', 'mimeType', 'src'))) {
         return null;
     }
     $accessToken = $this->getAccessToken();
     @set_time_limit(0);
     $http = new HttpClient(array('socketTimeout' => 10, 'streamTimeout' => 30, 'version' => HttpClient::HTTP_1_1));
     $http->setHeader('Authorization', "Bearer {$accessToken}");
     $endRange = $startRange + $chunkSize - 1;
     $http->setHeader('Range', "bytes={$startRange}-{$endRange}");
     if ($http->download($this->getDownloadUrl($fileData), $fileData->getSrc()) === false) {
         $errorString = implode('; ', array_keys($http->getError()));
         $this->errorCollection->add(array(new Error($errorString, self::ERROR_HTTP_DOWNLOAD_FILE)));
         return null;
     }
     return $fileData;
 }
Пример #3
0
 protected static function downloadFile($fileName, $storeAs, $skip404 = false, $storeTo = false)
 {
     // useless thing
     if (!$storeTo) {
         $storeTo = \CTempFile::GetDirectoryName(1);
     }
     $storeTo .= $storeAs;
     if (file_exists($storeTo)) {
         if (!is_writable($storeTo)) {
             throw new Main\SystemException('Cannot remove previous ' . $storeAs . ' file');
         }
         unlink($storeTo);
     }
     $query = 'http://' . self::DISTRIBUTOR_HOST . ':' . self::DISTRIBUTOR_PORT . self::REMOTE_PATH . $fileName;
     $client = new HttpClient();
     if (!$client->download($query, $storeTo)) {
         $eFormatted = array();
         foreach ($client->getError() as $code => $desc) {
             $eFormatted[] = trim($desc . ' (' . $code . ')');
         }
         throw new Main\SystemException('File download failed: ' . implode(', ', $eFormatted) . ' (' . $query . ')');
     }
     $status = intval($client->getStatus());
     if ($status != 200 && file_exists($storeTo)) {
         unlink($storeTo);
     }
     $okay = $status == 200 || $status == 404 && $skip404;
     // honestly we should check for all 2xx codes, but for now this is enough
     if (!$okay) {
         throw new Main\SystemException('File download failed: http error ' . $status . ' (' . $query . ')');
     }
     return filesize($storeTo);
 }
Пример #4
0
 protected static function downloadFile($fileName, $storeAs, $skip404 = false)
 {
     $storeTo = $_SERVER['DOCUMENT_ROOT'] . self::LOCAL_PATH;
     if (file_exists($storeTo)) {
         if (!is_writable($storeTo)) {
             throw new Main\SystemException('Temporal directory is not writable by the current user');
         }
     } else {
         $dir = new IO\Directory($_SERVER['DOCUMENT_ROOT']);
         $dir->createSubdirectory(self::LOCAL_PATH);
     }
     $storeTo .= $storeAs;
     if (file_exists($storeTo)) {
         if (!is_writable($storeTo)) {
             throw new Main\SystemException('Cannot remove previous ' . $storeAs . ' file');
         }
         unlink($storeTo);
     }
     $query = 'http://' . self::DISTRIBUTOR_HOST . ':' . self::DISTRIBUTOR_PORT . self::REMOTE_PATH . $fileName;
     $client = new HttpClient();
     if (!$client->download($query, $storeTo)) {
         $eFormatted = array();
         foreach ($client->getError() as $code => $desc) {
             $eFormatted[] = trim($desc . ' (' . $code . ')');
         }
         throw new Main\SystemException('File download failed: ' . implode(', ', $eFormatted) . ' (' . $query . ')');
     }
     $status = intval($client->getStatus());
     if ($status != 200 && file_exists($storeTo)) {
         unlink($storeTo);
     }
     $okay = $status == 200 || $status == 404 && $skip404;
     // honestly we should check for all 2xx codes, but for now this is enough
     if (!$okay) {
         throw new Main\SystemException('File download failed: http error ' . $status . ' (' . $query . ')');
     }
     // charset conversion here?
     return filesize($storeTo);
 }
Пример #5
0
 public function prepareUser($boxUser, $short = false)
 {
     $nameDetails = explode(" ", $boxUser['name'], 2);
     $id = $boxUser['id'];
     $arFields = array('EXTERNAL_AUTH_ID' => static::ID, 'XML_ID' => $id, 'LOGIN' => static::LOGIN_PREFIX . $id, 'NAME' => $nameDetails[0], 'LAST_NAME' => $nameDetails[1], 'EMAIL' => $boxUser["login"], 'OATOKEN' => $this->entityOAuth->getToken(), 'OATOKEN_EXPIRES' => $this->entityOAuth->getAccessTokenExpires(), 'REFRESH_TOKEN' => $this->entityOAuth->getRefreshToken());
     if (!$short && !empty($boxUser['avatar_url'])) {
         $picture_url = $boxUser['avatar_url'];
         $temp_path = CFile::GetTempName('', 'picture.jpg');
         $ob = new HttpClient(array("redirect" => true));
         $ob->download($picture_url, $temp_path);
         $arPic = CFile::MakeFileArray($temp_path);
         if ($arPic) {
             $arFields["PERSONAL_PHOTO"] = $arPic;
         }
     }
     if (strlen(SITE_ID) > 0) {
         $arFields["SITE_ID"] = SITE_ID;
     }
     return $arFields;
 }
Пример #6
0
 /**
  * @param string $url Image's URL.
  * @return integer Saved file identifier
  */
 protected static function saveImage($url)
 {
     $fileId = false;
     $file = new \CFile();
     $httpClient = new HttpClient();
     $httpClient->setTimeout(5);
     $httpClient->setStreamTimeout(5);
     $urlComponents = parse_url($url);
     if ($urlComponents && strlen($urlComponents["path"]) > 0) {
         $tempPath = $file->GetTempName('', bx_basename($urlComponents["path"]));
     } else {
         $tempPath = $file->GetTempName('', bx_basename($url));
     }
     $httpClient->download($url, $tempPath);
     $fileName = $httpClient->getHeaders()->getFilename();
     $localFile = \CFile::MakeFileArray($tempPath);
     if (is_array($localFile)) {
         if (strlen($fileName) > 0) {
             $localFile['name'] = $fileName;
         }
         if (\CFile::CheckImageFile($localFile, 0, 0, 0, array("IMAGE")) === null) {
             $fileId = $file->SaveFile($localFile, 'urlpreview', true);
         }
     }
     return $fileId === false ? null : $fileId;
 }
Пример #7
0
 /**
  * @return bool|string
  */
 protected function getArchive()
 {
     $client = new HttpClient();
     $fileName = pathinfo(self::ARCHIVE_URL, PATHINFO_BASENAME);
     if (!$client->download(self::ARCHIVE_URL, $_SERVER['DOCUMENT_ROOT'] . '/bitrix/tmp/' . $fileName)) {
         $this->errors = $client->getError();
         return false;
     } else {
         return $_SERVER['DOCUMENT_ROOT'] . '/bitrix/tmp/' . $fileName;
     }
 }