/** * Returns list of recently files by user. * @param mixed|int|User|\CAllUser $user User. * @param array $filter Filter. * @return File[] * @internal */ public function getFileModelListByUser($user, array $filter = array()) { $userId = User::resolveUserId($user); if (!$userId) { $this->errorCollection->addOne(new Error('Could not get user id.')); return array(); } $driver = Driver::getInstance(); $storage = $driver->getStorageByUserId($userId); if (!$storage) { $this->errorCollection->addOne(new Error('Could not get storage by user id.')); return array(); } if ($this->isFirstRun($userId)) { if (!$this->hasData($userId)) { $this->fixColdStart($userId); } $this->saveFirstRun($userId); } $securityContext = $storage->getCurrentUserSecurityContext(); $parameters = array('filter' => array('RECENTLY_USED.USER_ID' => $userId, 'DELETED_TYPE' => ObjectTable::DELETED_TYPE_NONE, 'TYPE' => ObjectTable::TYPE_FILE), 'order' => array('RECENTLY_USED.CREATE_TIME' => 'DESC'), 'limit' => RecentlyUsedTable::MAX_COUNT_FOR_USER); if ($filter) { $parameters['filter'] = array_merge($parameters['filter'], $filter); } $parameters = $driver->getRightsManager()->addRightsCheck($securityContext, $parameters, array('ID', 'CREATED_BY')); return File::getModelList($parameters); }
protected static function prepareDataToInsertFromFileArray(array $fileData, array $data, ErrorCollection $errorCollection) { list($relativePath, $absolutePath) = self::generatePath(); $file = new IO\File($fileData['tmp_name']); if (!$file->isExists()) { $errorCollection->addOne(new Error('Could not find file', self::ERROR_EXISTS_FILE)); return null; } if (!$file->rename($absolutePath)) { $errorCollection->addOne(new Error('Could not move file', self::ERROR_MOVE_FILE)); return null; } //now you can set CREATED_BY $data = array_intersect_key($data, array('CREATED_BY' => true)); return array_merge(array('TOKEN' => bx_basename($relativePath), 'FILENAME' => $fileData['name'], 'PATH' => $relativePath, 'BUCKET_ID' => '', 'SIZE' => '', 'IS_CLOUD' => '', 'WIDTH' => empty($fileData['width']) ? '' : $fileData['width'], 'HEIGHT' => empty($fileData['height']) ? '' : $fileData['height']), $data); }
public function uploadVersion(Entry $entry) { if (!$entry->getTmpFile()) { $this->errorCollection->addOne(new Error('Could not find cloud import', self::ERROR_COULD_NOT_FIND_CLOUD_IMPORT)); return null; } if ($entry->getContentSize() != $entry->getDownloadedContentSize()) { $this->errorCollection->addOne(new Error('Content size != downloaded content size')); return null; } /** @var File $file */ $file = $entry->getObject(); if (!$file) { $this->errorCollection->addOne(new Error('Could not get file from cloud import record')); return null; } $tmpFile = $entry->getTmpFile(); $fileArray = \CFile::makeFileArray($tmpFile->getAbsolutePath()); $version = $file->uploadVersion($fileArray, $this->documentHandler->getUserId()); if (!$version) { $tmpFile->delete(); $this->errorCollection->add($file->getErrors()); return null; } $entry->linkVersion($version); return $version; }
/** * Clones uf values from entity and creates new files (copies from attach) to save in new entity. * @param array $attachedIds List of attached objects id. * @param int $userId Id of user. * @internal * @return array */ public function cloneUfValuesFromAttachedObject(array $attachedIds, $userId) { $this->errorCollection->clear(); $userId = (int) $userId; if ($userId <= 0) { $this->errorCollection->addOne(new Error('Invalid $userId')); return null; } $userStorage = Driver::getInstance()->getStorageByUserId($userId); if (!$userStorage) { $this->errorCollection->addOne(new Error("Could not find storage for user {$userId}")); $this->errorCollection->add(Driver::getInstance()->getErrors()); return null; } $folder = $userStorage->getFolderForUploadedFiles(); if (!$folder) { $this->errorCollection->addOne(new Error("Could not create/find folder for upload")); $this->errorCollection->add($userStorage->getErrors()); return null; } $newValues = array(); foreach ($attachedIds as $id) { list($type, $realValue) = FileUserType::detectType($id); if (FileUserType::TYPE_ALREADY_ATTACHED != $type) { continue; } $attachedObject = AttachedObject::loadById($realValue, array('OBJECT')); if (!$attachedObject) { continue; } if (!$attachedObject->canRead($userId)) { continue; } $file = $attachedObject->getFile(); if (!$file) { continue; } $newFile = $file->copyTo($folder, $userId, true); if (!$newFile) { $this->errorCollection->add($file->getErrors()); continue; } $newValues[] = FileUserType::NEW_FILE_PREFIX . $newFile->getId(); } return $newValues; }
/** * Rollback upload by token. Destroy temporary content. * @return bool Status of delete. */ public function rollbackByToken() { if (!$this->hasToken()) { $this->errorCollection->addOne(new Error("Could not delete content file by token. Have to set token parameter.", self::ERROR_EMPTY_TOKEN)); return false; } $tmpFile = $this->findUserSpecificTmpFileByToken(); if (!$tmpFile) { $this->errorCollection->addOne(new Error("Could not find file by token", self::ERROR_UNKNOWN_TOKEN)); return false; } $success = $tmpFile->delete(); if (!$success) { $this->errorCollection->add($tmpFile->getErrors()); } return $success; }
protected static function prepareDataToInsertFromFileArray(array $fileData, array $data, ErrorCollection $errorCollection) { if (($fileData['error'] = intval($fileData['error'])) > 0) { if ($fileData['error'] < 3) { $errorCollection->addOne(new Error('upload_max_filesize: ' . intval(ini_get('upload_max_filesize')), static::ERROR_UPLOAD_MAX_FILE_SIZE)); return null; } $errorCollection->addOne(new Error('upload_error ' . $fileData['error'], static::ERROR_UPLOAD_FILE)); return null; } if (!is_uploaded_file($fileData['tmp_name'])) { $errorCollection->addOne(new Error('Current file is unsafe (is_uploaded_file check)', static::ERROR_IS_NOT_UPLOADED_FILE)); return null; } list($relativePath, $absolutePath) = static::generatePath(); if (!move_uploaded_file($fileData['tmp_name'], $absolutePath)) { $errorCollection->addOne(new Error('Could not move uploaded file (move_uploaded_file)', static::ERROR_MOVE_UPLOADED_FILE)); return null; } //now you can set CREATED_BY $data = array_intersect_key($data, array('CREATED_BY' => true)); return array_merge(array('TOKEN' => bx_basename($relativePath), 'FILENAME' => $fileData['name'], 'CONTENT_TYPE' => empty($fileData['type']) ? \CFile::getContentType($absolutePath) : $fileData['type'], 'PATH' => $relativePath, 'BUCKET_ID' => '', 'SIZE' => '', 'IS_CLOUD' => '', 'WIDTH' => empty($fileData['width']) ? '' : $fileData['width'], 'HEIGHT' => empty($fileData['height']) ? '' : $fileData['height']), $data); }
/** * Connects object from [[$data['REAL_OBJECT']]] (or by id from [[$data['REAL_OBJECT_ID']]]) to user storage. * * If set $data['SELF_CONNECT'] to true, then doesn't ask about connecting object. * * @param int $userId Id of user. * @param array $data Data. * @param ErrorCollection $errorCollection Error collection. * @return Sharing|null * @throws \Bitrix\Main\NotImplementedException * @throws \Bitrix\Main\SystemException */ public static function connectToUserStorage($userId, array $data, ErrorCollection $errorCollection) { $storage = Driver::getInstance()->getStorageByUserId($userId); if (!$storage) { $storage = Driver::getInstance()->addUserStorage($userId); } if (!$storage) { $errorCollection->addOne(new Error(Loc::getMessage('DISK_SHARING_MODEL_ERROR_COULD_NOT_FIND_USER_STORAGE', array('#USER_ID#' => $userId)), self::ERROR_COULD_NOT_FIND_USER_STORAGE)); return null; } $selfConnect = !empty($data['SELF_CONNECT']); $linkName = !empty($data['LINK_NAME']) ? $data['LINK_NAME'] : null; unset($data['SELF_CONNECT'], $data['LINK_NAME']); $data['TYPE'] = SharingTable::TYPE_TO_USER; $data['FROM_ENTITY'] = self::CODE_USER . (int) $userId; $data['TO_ENTITY'] = self::CODE_USER . (int) $userId; $data['TASK_NAME'] = RightsManager::TASK_READ; if (isset($data['REAL_OBJECT']) && $data['REAL_OBJECT'] instanceof BaseObject) { /** @noinspection PhpUndefinedMethodInspection */ $data['REAL_OBJECT_ID'] = $data['REAL_OBJECT']->getId(); } elseif (isset($data['REAL_OBJECT_ID'])) { $data['REAL_OBJECT'] = BaseObject::loadById($data['REAL_OBJECT_ID']); } else { $errorCollection->addOne(new Error(Loc::getMessage('DISK_SHARING_MODEL_ERROR_EMPTY_REAL_OBJECT'), self::ERROR_EMPTY_REAL_OBJECT)); return null; } /** @var \Bitrix\Disk\BaseObject $objectToSharing */ $objectToSharing = $data['REAL_OBJECT']; //resolve to last real object. In table we write only real (not link) id. $objectToSharing = $objectToSharing->getRealObject(); $data['REAL_OBJECT_ID'] = $objectToSharing->getId(); $data['REAL_STORAGE_ID'] = $objectToSharing->getStorageId(); $dataToInsert = $data; unset($dataToInsert['REAL_OBJECT']); $sharingModel = parent::add($dataToInsert, $errorCollection); if (!$sharingModel) { return null; } $sharingModel->setAttributes(array('REAL_OBJECT' => $objectToSharing)); if (!$selfConnect) { self::processConnectAndNotify(array($sharingModel), $objectToSharing); } else { if (!$sharingModel->approve(array('LINK_NAME' => $linkName))) { $errorCollection->add($sharingModel->getErrors()); $sharingModel->delete($userId); return null; } } return $sharingModel; }
private function appendOneNegative(array $right, $hadOppositeRight = false) { $isValidNegaviteRight = $this->validateNegaviteRight($right); if (!$isValidNegaviteRight && !$hadOppositeRight) { $this->errorCollection->addOne(new Error('Invalid negative right')); return false; } //we don't have to add negative right. We must only delete old simple rights. condition($hadOppositeRight && !$isValidNegaviteRight) if ($isValidNegaviteRight) { //May we have to add record to b_disk_right in final. $right['OBJECT_ID'] = $this->object->getId(); $result = RightTable::add($right); if (!$result->isSuccess()) { $this->errorCollection->addFromResult($result); return false; } } $rightsManager = Driver::getInstance()->getRightsManager(); if (!$rightsManager->containsOperationInTask($rightsManager::OP_READ, $right['TASK_ID'])) { return true; } if (!$this->hasAlreadySimpleRight($right['ACCESS_CODE'])) { //below we already have negative rights, which deleted simple rights. return true; } //need to delete simple rights from descendants $conflictRightsInSubTree = $this->getConflictRightsInSubTree($right['ACCESS_CODE'], $right['TASK_ID']); $accessCode = $this->sqlHelper->forSql($right['ACCESS_CODE']); if (empty($conflictRightsInSubTree)) { //we have to destroy simple right from all descendants and from current OBJECT_ID if ($this->connection instanceof OracleConnection) { $this->connection->queryExecute("\n\t\t\t\t\tDELETE FROM (SELECT simple.* FROM b_disk_simple_right simple\n\t\t\t\t\t\tINNER JOIN b_disk_object_path p ON p.OBJECT_ID = simple.OBJECT_ID\n\t\t\t\t\tWHERE p.PARENT_ID = {$this->object->getId()} AND simple.ACCESS_CODE = '{$accessCode}')\n\t\t\t\t"); } else { $this->connection->queryExecute("\n\t\t\t\t\tDELETE simple FROM b_disk_simple_right simple\n\t\t\t\t\t\tINNER JOIN b_disk_object_path p ON p.OBJECT_ID = simple.OBJECT_ID\n\t\t\t\t\tWHERE p.PARENT_ID = {$this->object->getId()} AND simple.ACCESS_CODE = '{$accessCode}'\n\t\t\t\t"); } } else { $objectIds = array(); foreach ($conflictRightsInSubTree as $conflictRight) { $objectIds[] = $conflictRight['OBJECT_ID']; } unset($conflictRight); //we have to destroy simple right from all descendants and from current OBJECT_ID without nodes with conflict rights in path. if ($this->connection instanceof OracleConnection) { $this->connection->queryExecute("\n\t\t\t\t\tDELETE FROM (SELECT simple.* FROM b_disk_simple_right simple\n\t\t\t\t\t\tINNER JOIN b_disk_object_path p ON p.OBJECT_ID = simple.OBJECT_ID\n\t\t\t\t\tWHERE\n\t\t\t\t\t\tp.PARENT_ID = {$this->object->getId()} AND simple.ACCESS_CODE = '{$accessCode}' AND\n\t\t\t\t\t\tNOT EXISTS(\n\t\t\t\t\t\t\tSELECT 'x' FROM b_disk_object_path pp\n\t\t\t\t\t\t\t\tWHERE pp.OBJECT_ID = p.OBJECT_ID AND\n\t\t\t\t\t\t\t\tpp.PARENT_ID IN (" . implode(',', $objectIds) . ") ))\n\t\t\t\t"); } else { $this->connection->queryExecute("\n\t\t\t\t\tDELETE simple FROM b_disk_simple_right simple\n\t\t\t\t\t\tINNER JOIN b_disk_object_path p ON p.OBJECT_ID = simple.OBJECT_ID\n\t\t\t\t\tWHERE\n\t\t\t\t\t\tp.PARENT_ID = {$this->object->getId()} AND simple.ACCESS_CODE = '{$accessCode}' AND\n\t\t\t\t\t\tNOT EXISTS(\n\t\t\t\t\t\t\tSELECT 'x' FROM b_disk_object_path pp\n\t\t\t\t\t\t\t\tWHERE pp.OBJECT_ID = p.OBJECT_ID AND\n\t\t\t\t\t\t\t\tpp.PARENT_ID IN (" . implode(',', $objectIds) . ") )\n\t\t\t\t"); } } return true; }