/** * Convert an IShare to an array for OCS output * * @param IShare $share * @return array */ protected function formatShare($share) { $result = ['id' => $share->getId(), 'share_type' => $share->getShareType(), 'uid_owner' => $share->getSharedBy()->getUID(), 'displayname_owner' => $share->getSharedBy()->getDisplayName(), 'permissions' => $share->getPermissions(), 'stime' => $share->getShareTime(), 'parent' => $share->getParent(), 'expiration' => null, 'token' => null]; $path = $share->getPath(); $result['path'] = $this->userFolder->getRelativePath($path->getPath()); if ($path instanceof \OCP\Files\Folder) { $result['item_type'] = 'folder'; } else { $result['item_type'] = 'file'; } $result['storage_id'] = $path->getStorage()->getId(); $result['storage'] = \OC\Files\Cache\Storage::getNumericStorageId($path->getStorage()->getId()); $result['item_source'] = $path->getId(); $result['file_source'] = $path->getId(); $result['file_parent'] = $path->getParent()->getId(); $result['file_target'] = $share->getTarget(); if ($share->getShareType() === \OCP\Share::SHARE_TYPE_USER) { $sharedWith = $share->getSharedWith(); $result['share_with'] = $sharedWith->getUID(); $result['share_with_displayname'] = $sharedWith->getDisplayName(); } else { if ($share->getShareType() === \OCP\Share::SHARE_TYPE_GROUP) { $sharedWith = $share->getSharedWith(); $result['share_with'] = $sharedWith->getGID(); $result['share_with_displayname'] = $sharedWith->getGID(); } else { if ($share->getShareType() === \OCP\Share::SHARE_TYPE_LINK) { $result['share_with'] = $share->getPassword(); $result['share_with_displayname'] = $share->getPassword(); $result['token'] = $share->getToken(); $result['url'] = $this->urlGenerator->linkToRouteAbsolute('files_sharing.sharecontroller.showShare', ['token' => $share->getToken()]); $expiration = $share->getExpirationDate(); if ($expiration !== null) { $result['expiration'] = $expiration->format('Y-m-d 00:00:00'); } } else { if ($share->getShareType() === \OCP\Share::SHARE_TYPE_REMOTE) { $result['share_with'] = $share->getSharedWith(); $result['share_with_displayname'] = $share->getSharedWith(); $result['token'] = $share->getToken(); } } } } $result['mail_send'] = $share->getMailSend() ? 1 : 0; return $result; }
/** * Verify the password of a public share * * @param IShare $share * @param string $password * @return bool */ public function checkPassword(IShare $share, $password) { if ($share->getShareType() !== \OCP\Share::SHARE_TYPE_LINK) { //TODO maybe exception? return false; } if ($password === null || $share->getPassword() === null) { return false; } $newHash = ''; if (!$this->hasher->verify($password, $share->getPassword(), $newHash)) { return false; } if (!empty($newHash)) { //TODO update hash! } return true; }
/** * Share a path * * @param IShare $share * @return IShare The share object * @throws ShareNotFound * @throws \Exception */ public function create(IShare $share) { $qb = $this->dbConn->getQueryBuilder(); $qb->insert('share'); $qb->setValue('share_type', $qb->createNamedParameter($share->getShareType())); if ($share->getShareType() === \OCP\Share::SHARE_TYPE_USER) { //Set the UID of the user we share with $qb->setValue('share_with', $qb->createNamedParameter($share->getSharedWith()->getUID())); } else { if ($share->getShareType() === \OCP\Share::SHARE_TYPE_GROUP) { //Set the GID of the group we share with $qb->setValue('share_with', $qb->createNamedParameter($share->getSharedWith()->getGID())); } else { if ($share->getShareType() === \OCP\Share::SHARE_TYPE_LINK) { //Set the token of the share $qb->setValue('token', $qb->createNamedParameter($share->getToken())); //If a password is set store it if ($share->getPassword() !== null) { $qb->setValue('share_with', $qb->createNamedParameter($share->getPassword())); } //If an expiration date is set store it if ($share->getExpirationDate() !== null) { $qb->setValue('expiration', $qb->createNamedParameter($share->getExpirationDate(), 'datetime')); } } else { throw new \Exception('invalid share type!'); } } } // Set what is shares $qb->setValue('item_type', $qb->createParameter('itemType')); if ($share->getPath() instanceof \OCP\Files\File) { $qb->setParameter('itemType', 'file'); } else { $qb->setParameter('itemType', 'folder'); } // Set the file id $qb->setValue('item_source', $qb->createNamedParameter($share->getPath()->getId())); $qb->setValue('file_source', $qb->createNamedParameter($share->getPath()->getId())); // set the permissions $qb->setValue('permissions', $qb->createNamedParameter($share->getPermissions())); // Set who created this share $qb->setValue('uid_initiator', $qb->createNamedParameter($share->getSharedBy()->getUID())); // Set who is the owner of this file/folder (and this the owner of the share) $qb->setValue('uid_owner', $qb->createNamedParameter($share->getShareOwner()->getUID())); // Set the file target $qb->setValue('file_target', $qb->createNamedParameter($share->getTarget())); // Set the time this share was created $qb->setValue('stime', $qb->createNamedParameter(time())); // insert the data and fetch the id of the share $this->dbConn->beginTransaction(); $qb->execute(); $id = $this->dbConn->lastInsertId('*PREFIX*share'); $this->dbConn->commit(); // Now fetch the inserted share and create a complete share object $qb = $this->dbConn->getQueryBuilder(); $qb->select('*')->from('*PREFIX*share')->where($qb->expr()->eq('id', $qb->createNamedParameter($id))); $cursor = $qb->execute(); $data = $cursor->fetch(); $cursor->closeCursor(); if ($data === false) { throw new ShareNotFound(); } $share = $this->createShare($data); return $share; }
/** * Update a share * * @param IShare $share * @return IShare The share object */ public function update(IShare $share) { if ($share->getShareType() === \OCP\Share::SHARE_TYPE_USER) { /* * We allow updating the recipient on user shares. */ $qb = $this->dbConn->getQueryBuilder(); $qb->update('share')->where($qb->expr()->eq('id', $qb->createNamedParameter($share->getId())))->set('share_with', $qb->createNamedParameter($share->getSharedWith()->getUID()))->set('uid_owner', $qb->createNamedParameter($share->getShareOwner()->getUID()))->set('uid_initiator', $qb->createNamedParameter($share->getSharedBy()->getUID()))->set('permissions', $qb->createNamedParameter($share->getPermissions()))->set('item_source', $qb->createNamedParameter($share->getPath()->getId()))->set('file_source', $qb->createNamedParameter($share->getPath()->getId()))->execute(); } else { if ($share->getShareType() === \OCP\Share::SHARE_TYPE_GROUP) { $qb = $this->dbConn->getQueryBuilder(); $qb->update('share')->where($qb->expr()->eq('id', $qb->createNamedParameter($share->getId())))->set('uid_owner', $qb->createNamedParameter($share->getShareOwner()->getUID()))->set('uid_initiator', $qb->createNamedParameter($share->getSharedBy()->getUID()))->set('permissions', $qb->createNamedParameter($share->getPermissions()))->set('item_source', $qb->createNamedParameter($share->getPath()->getId()))->set('file_source', $qb->createNamedParameter($share->getPath()->getId()))->execute(); /* * Update all user defined group shares */ $qb = $this->dbConn->getQueryBuilder(); $qb->update('share')->where($qb->expr()->eq('parent', $qb->createNamedParameter($share->getId())))->set('uid_owner', $qb->createNamedParameter($share->getShareOwner()->getUID()))->set('uid_initiator', $qb->createNamedParameter($share->getSharedBy()->getUID()))->set('item_source', $qb->createNamedParameter($share->getPath()->getId()))->set('file_source', $qb->createNamedParameter($share->getPath()->getId()))->execute(); /* * Now update the permissions for all children that have not set it to 0 */ $qb = $this->dbConn->getQueryBuilder(); $qb->update('share')->where($qb->expr()->eq('parent', $qb->createNamedParameter($share->getId())))->andWhere($qb->expr()->neq('permissions', $qb->createNamedParameter(0)))->set('permissions', $qb->createNamedParameter($share->getPermissions()))->execute(); } else { if ($share->getShareType() === \OCP\Share::SHARE_TYPE_LINK) { $qb = $this->dbConn->getQueryBuilder(); $qb->update('share')->where($qb->expr()->eq('id', $qb->createNamedParameter($share->getId())))->set('share_with', $qb->createNamedParameter($share->getPassword()))->set('uid_owner', $qb->createNamedParameter($share->getShareOwner()->getUID()))->set('uid_initiator', $qb->createNamedParameter($share->getSharedBy()->getUID()))->set('permissions', $qb->createNamedParameter($share->getPermissions()))->set('item_source', $qb->createNamedParameter($share->getPath()->getId()))->set('file_source', $qb->createNamedParameter($share->getPath()->getId()))->set('token', $qb->createNamedParameter($share->getToken()))->set('expiration', $qb->createNamedParameter($share->getExpirationDate(), IQueryBuilder::PARAM_DATE))->execute(); } } } return $share; }