예제 #1
1
 protected static function uploadFile($name, $content, array $parameters = array())
 {
     static::includeDisk();
     $storage = Driver::getInstance()->getStorageByUserId($parameters['USER_ID']);
     if (!$storage) {
         throw new RestException("Could not find storage for user '" . $parameters['USER_ID'] . "'.", RestException::ERROR_NOT_FOUND);
     }
     $folder = $storage->getFolderForUploadedFiles();
     if (!$folder) {
         return false;
     }
     $securityContext = $storage->getCurrentUserSecurityContext();
     if (!$folder->canAdd($securityContext)) {
         throw new AccessException();
     }
     $fileData = \CRestUtil::saveFile($content);
     if (!$fileData) {
         throw new RestException('Could not save file');
     }
     $file = $folder->uploadFile($fileData, array('NAME' => $name, 'CREATED_BY' => $parameters['USER_ID']), array(), true);
     if (!$file) {
         //$folder->getErrors();
         throw new RestException("Could not upload file to the storage");
     }
     return $file->getId();
 }
예제 #2
0
 public static function mailserviceUpdate($arParams)
 {
     global $USER;
     IncludeModuleLangFile(__FILE__);
     if (!$USER->CanDoOperation('bitrix24_config')) {
         throw new Exception(GetMessage('ACCESS_DENIED'));
     }
     if (empty($arParams['ID'])) {
         throw new Exception(GetMessage('MAIL_MAILSERVICE_EMPTY_ID'));
     }
     $result = Bitrix\Mail\MailServicesTable::getList(array('filter' => array('=ID' => $arParams['ID'], '=SITE_ID' => SITE_ID)));
     if (!$result->fetch()) {
         throw new Exception(GetMessage('MAIL_MAILSERVICE_EMPTY'));
     }
     $arFields = array('ACTIVE' => $arParams['ACTIVE'], 'NAME' => $arParams['NAME'], 'SERVER' => $arParams['SERVER'], 'PORT' => $arParams['PORT'], 'ENCRYPTION' => $arParams['ENCRYPTION'], 'LINK' => $arParams['LINK'], 'ICON' => CRestUtil::saveFile($arParams['ICON']) ?: $arParams['ICON'], 'SORT' => $arParams['SORT']);
     foreach ($arFields as $name => $value) {
         if (empty($value)) {
             unset($arFields[$name]);
         }
     }
     $result = Bitrix\Mail\MailServicesTable::update($arParams['ID'], $arFields);
     if (!$result->isSuccess()) {
         throw new Exception(join('; ', $result->getErrorMessages()));
     }
     return true;
 }
예제 #3
0
 public static function AddBlogPost($arFields)
 {
     if (!is_array($_POST)) {
         $_POST = array();
     }
     $_POST = array_merge($_POST, array("apply" => "Y", "decode" => "N"), $arFields);
     $strPathToPost = COption::GetOptionString("socialnetwork", "userblogpost_page", false, SITE_ID);
     $strPathToSmile = COption::GetOptionString("socialnetwork", "smile_page", false, SITE_ID);
     $BlogGroupID = COption::GetOptionString("socialnetwork", "userbloggroup_id", false, SITE_ID);
     $arBlogComponentParams = array("IS_REST" => "Y", "ID" => "new", "PATH_TO_POST" => $strPathToPost, "PATH_TO_SMILE" => $strPathToSmile, "GROUP_ID" => $BlogGroupID, "USER_ID" => $GLOBALS["USER"]->GetID(), "USE_SOCNET" => "Y", "MICROBLOG" => "Y");
     ob_start();
     $result = $GLOBALS["APPLICATION"]->IncludeComponent("bitrix:socialnetwork.blog.post.edit", "", $arBlogComponentParams, false, array("HIDE_ICONS" => "Y"));
     ob_end_clean();
     if (!$result) {
         throw new Exception('Error');
     } else {
         if (isset($arFields["FILES"]) && \Bitrix\Main\Config\Option::get('disk', 'successfully_converted', false) && CModule::includeModule('disk') && ($storage = \Bitrix\Disk\Driver::getInstance()->getStorageByUserId($GLOBALS["USER"]->GetID())) && ($folder = $storage->getFolderForUploadedFiles($GLOBALS["USER"]->GetID()))) {
             // upload to storage
             $arResultFile = array();
             foreach ($arFields["FILES"] as $tmp) {
                 $arFile = CRestUtil::saveFile($tmp);
                 if (is_array($arFile)) {
                     $file = $folder->uploadFile($arFile, array('NAME' => $arFile["name"], 'CREATED_BY' => $GLOBALS["USER"]->GetID()), array(), true);
                     if ($file) {
                         $arResultFile[] = \Bitrix\Disk\Uf\FileUserType::NEW_FILE_PREFIX . $file->getId();
                     }
                 }
             }
             if (!empty($arResultFile)) {
                 CBlogPost::Update($result, array("HAS_PROPS" => "Y", "UF_BLOG_POST_FILE" => $arResultFile));
             }
         }
         return $result;
     }
 }
예제 #4
0
 /**
  * Creates new version of file.
  * @param int $id Id of file.
  * @param string|array $fileContent File content. General format in REST.
  * @return Disk\Version|null
  * @throws AccessException
  * @throws RestException
  */
 protected function uploadVersion($id, $fileContent)
 {
     $file = $this->getFileById($id);
     $securityContext = $file->getStorage()->getCurrentUserSecurityContext();
     if (!$file->canUpdate($securityContext)) {
         throw new AccessException();
     }
     $fileData = \CRestUtil::saveFile($fileContent);
     if (!$fileData) {
         throw new RestException('Could not save file.');
     }
     $newFile = $file->uploadVersion($fileData, $this->userId);
     if (!$newFile) {
         $this->errorCollection->add($file->getErrors());
         return null;
     }
     return $file;
 }
예제 #5
0
 /**
  * Creates new file in folder.
  * @param       int    $id          Id of folder.
  * @param string|array $fileContent File content. General format in REST.
  * @param array        $data        Data for new file.
  * @param array        $rights      Specific rights on file. If empty, then use parents rights.
  * @return Disk\File|null
  * @throws AccessException
  * @throws RestException
  */
 protected function uploadFile($id, $fileContent, array $data, array $rights = array())
 {
     if (!$this->checkRequiredInputParams($data, array('NAME'))) {
         return null;
     }
     $folder = $this->getFolderById($id);
     $securityContext = $folder->getStorage()->getCurrentUserSecurityContext();
     if (!$folder->canAdd($securityContext)) {
         throw new AccessException();
     }
     $fileData = \CRestUtil::saveFile($fileContent);
     if (!$fileData) {
         throw new RestException('Could not save file.');
     }
     $file = $folder->uploadFile($fileData, array('NAME' => $data['NAME'], 'CREATED_BY' => $this->userId), $rights);
     if (!$file) {
         $this->errorCollection->add($folder->getErrors());
         return null;
     }
     return $file;
 }
예제 #6
0
 protected function tryInternalizeDiskFileField(&$fields, $fieldName, $multiple = false)
 {
     if (!isset($fields[$fieldName])) {
         return false;
     }
     $result = array();
     $values = $multiple && self::isIndexedArray($fields[$fieldName]) ? $fields[$fieldName] : array($fields[$fieldName]);
     foreach ($values as &$v) {
         if (!self::isAssociativeArray($v)) {
             continue;
         }
         $fileID = isset($v['id']) ? intval($v['id']) : 0;
         $removeElement = isset($v['remove']) && is_string($v['remove']) && strtoupper($v['remove']) === 'Y';
         $fileData = isset($v['fileData']) ? $v['fileData'] : '';
         if (!self::isIndexedArray($fileData)) {
             continue;
         }
         $fileDataLength = count($fileData);
         if ($fileDataLength === 0) {
             continue;
         }
         if ($fileDataLength === 1) {
             $fileName = '';
             $fileContent = $fileData[0];
         } else {
             $fileName = $fileData[0];
             $fileContent = $fileData[1];
         }
         if (is_string($fileContent) && $fileContent !== '') {
             $fileInfo = CRestUtil::saveFile($fileContent, $fileName);
             $folder = DiskManager::ensureFolderCreated(StorageFileType::Rest);
             if (!$folder) {
                 unlink($fileInfo['tmp_name']);
                 throw new RestException('Could not create disk folder for rest files.');
             }
             $file = $folder->uploadFile($fileInfo, array('NAME' => $fileName, 'CREATED_BY' => $this->getCurrentUserID()), array(), true);
             unlink($fileInfo['tmp_name']);
             if (!$file) {
                 throw new RestException('Could not create disk file.');
             }
             $result[] = array('FILE_ID' => $file->getId());
         } elseif ($fileID > 0 && $removeElement) {
             $result[] = array('OLD_FILE_ID' => $fileID, 'DELETE' => true);
         }
     }
     unset($v);
     if ($multiple) {
         $fields[$fieldName] = $result;
         return true;
     } elseif (!empty($result)) {
         $fields[$fieldName] = $result[0];
         return true;
     }
     unset($fields[$fieldName]);
     return false;
 }